Skip to content

Instantly share code, notes, and snippets.

@domwom
Created October 19, 2009 17:45
Show Gist options
  • Save domwom/213542 to your computer and use it in GitHub Desktop.
Save domwom/213542 to your computer and use it in GitHub Desktop.
php bitly api
function make_bitly_url($url, $login, $appkey, $format='xml', $history=1, $version='2.0.1')
{
//create the URL
$bitly = 'http://api.bit.ly/shorten';
$param = 'version='.$version.'&longUrl='.urlencode($url).'&login='
.$login.'&apiKey='.$appkey.'&format='.$format.'&history='.$history;
//get the url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $bitly . "?" . $param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
//parse depending on desired format
if(strtolower($format) == 'json') {
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
} else {
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
@squiter
Copy link

squiter commented Jun 7, 2011

Thanks, this script helped me a lot! ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment