Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Created March 8, 2013 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcelestino/5119170 to your computer and use it in GitHub Desktop.
Save tcelestino/5119170 to your computer and use it in GitHub Desktop.
shorten url using bit.ly api
<?php
//função do curl baseado no script do David Walsh (http://davidwalsh.name/)
function curl_get_result($url) {
$ch = curl_init($url);
$timeout = 50;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function shortURL($link) {
$format = "json";
$login = "your-login";
$apiKey = "insert_your_apikey";
$bitly = 'https://api-ssl.bitly.com/v3/shorten?login='.$login.'&apiKey='.$apiKey.'&longUrl='.urldecode($link).'&format='.$format;
$getURL = curl_get_result($bitly);
$json = @json_decode($getURL, true);
return $json['data']['url'];
}
echo shortURL("http://google.com"); // example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment