Behind the bit.ly url

By 08 Jul 2010 | Comment
If you want to know more about what a bit.ly url refers to, here is a PHP function that may be of use to you. It first checks what the destination URL is, and what the title is. Unfortunately this requires two different bit.ly API calls.

Note:

  • You can ask for 15 different URLs in each call, if you have a bunch that you need to analyse.
  • The URL returned is not necessarily the 'final' one, and may still need to be checked (by a separate HTTP request) for a Location: response in its header.
function bitly($url,&$title) { $login="mylogin"; $apikey="my-api-key"; $bitlyurl="http://api.bit.ly/v3/expand?shortUrl=".urlencode($url)."&login=$login&apiKey=$apikey&format=json"; $inf=json_decode(simpleget($bitlyurl),true); $code=$inf["status_code"]; if($code != 200) { print "bitly($url) Error code $code\n"; return; } $ret=$inf["data"]["expand"][0]["long_url"]; $bitlyurl="http://api.bit.ly/v3/info?shortUrl=".urlencode($url)."&login=$login&apiKey=$apikey&format=json"; $inf=json_decode(simpleget($bitlyurl),true); $code=$inf["status_code"]; if($code != 200) { print "bitly($url) Error code $code\n"; return; } $title=$inf["data"]["info"][0]["title"]; print "bitly($url) title=$title ; url=$ret\n"; return $ret; } function simpleget($url) { $ch=curl_init($url); curl_setopt($ch, CURLOPT_TIMEOUT, 6); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_USERAGENT, "simpleget"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); return curl_exec($ch); }

References


Comments

It's quiet in here...Add your comment

Web Development Survey!
Which web technologies would you like to see the back of, and why?