/ Published in: PHP
there are numbers of url shortening services available these days, including the good old tinyurl and something really short like u.nu. now when you get the short url shortened by using any of these services, you dont know where your browser is taking you! so if you are interested to figure out the original url hiding behind these short url, you need to have a little knowledge on how these services actually work. if you go to any of these short urls, they tell your browser “HTTP 30X: Object has moved†HTTP HEADER (optionally, some does it, some doesn’t) and then asks your browser to move to the original url using “Location†in HTTP HEADER. so all you have to do is just get the HTTP HEADER out first (PHP and Curl is pretty good at doing this, heh heh) and then parse the “Location†parameter from it.
lets see how that works in code
lets see how that works in code
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
< ?php $url = "http://tinyurl.com/2dfmty"; echo "Short URL: {$url}<br/>"; echo "Original URL: {$pdata['Location']}"; { foreach( $fields as $field ) { $match[1] = preg_replace('/(?< =^|[\x09\x20\x2D])./e', 'strtoupper("")', strtolower(trim($match[1]))); } else { } } } return $retVal; } ?> now you see that the output of this code is Short URL: http://tinyurl.com/2dfmty Original URL: http://ghill.customer.netspace.net.au/embiggen/
URL: http://hasin.me/2009/05/05/expanding-short-urls-to-original-urls-using-php-and-curl/