Resolving TinyURLS to the desination URL
After Twitter hugged TinyURL, there happens to be lots of talk about how to enhance TinyURL services.
for example:
http://preview.tinyurl.com/37dz8l
redirects to
http://www.webforth.com/
But how do we understand that the first URL is going to get redirected to the second URL. It is pretty clear that the string 37dz8l does the magic. TinyURL folk has written a smart algorithm to shrink bigger URLs to a hashed string (Anybody know how that works?). For the time being, I wrote a small snippet for resolving a tiny url to its original destination URL. The script will directly contact TinyURL server to see where the URL is getting redirected.
< ?php
// tinyurl.php?c=
$num = $_GET['c'];
if($fp = fsockopen ("tinyurl.com", 80, $errno, $errstr, 30))
{
if ($fp) {
fputs ($fp, "HEAD /$num HTTP/1.0\r\nHost: tinyurl.com\r\n\r\n");
while (!feof($fp)) {$headers .= fgets ($fp,128);}
fclose ($fp);
}
$arr1=explode("Location:",$headers);
$arr=explode("\n",trim($arr1[1]));
echo trim($arr[0]);
}
?>
It is a simple code which plays with http headers rather than HTML. It processes shorter strings and string manipulation is done using non-regexp tricks.









If you aware of, 













