简体   繁体   中英

How can I get a 302 redirection URL's Location header in PHP?

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

Forget it everyone. :) With some internet searching, I found this:

expanding short url to original url using PHP and CURL — Hasin Hayder

It shows me exactly how to do this.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM