Created
October 19, 2009 17:45
-
-
Save domwom/213542 to your computer and use it in GitHub Desktop.
php bitly api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this script helped me a lot! ;)