Behind the url

If you want to know more about what a 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 API calls.


  • 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="".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="".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); }



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