views:

37

answers:

2

I am trying to find a universal way to expand most if not all of the shortened URLs out there. I know short URLs such as bit.ly, TinyURL, goo.gl, etc use the 302 redirection method to redirect you to another site. How can I make a HEAD request to the shortened URL in php and get the "Location" part of the header?

Please help me with this.

Thanks

+1  A: 

Forget it everyone. :) With some internet searching, I found this: http://hasin.wordpress.com/2009/05/05/expanding-short-urls-to-original-urls-using-php-and-curl/

It shows me exactly how to do this.

Thanks

QAH
Ah, I was typing an answer which explains less or more the same. Good you found it out yourself in meanwhile. By the way, to make it a real `HEAD` request, add `curl_setopt($ch, CURLOPT_NOBODY, true);`.
BalusC
A: 

You need to use CURL. You can set a callback function that fires to read headers.

//register a callback function which will process the headers
 curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'readHeader');


function readHeader($ch, $header)
{ 
    global $location;

    // we have to follow 302s automatically or cookies get lost.
    if (eregi("Location:",$header) )
    {
        $location= substr($header,strlen("Location: "));
    }

    return strlen($header);
}
Byron Whitlock