How can I get the URL redirect URL 302 in PHP?

I am trying to find a universal way to extend most, if not all, shortened URLs. I know short URLs like bit.ly, TinyURL, goo.gl, etc. Use the 302 redirect method to redirect you to another site. How can I make a HEAD request to an abbreviated url in php and get the "Location" part in the header?

Please help me with this.

thank

+3
source share
2 answers

Forget it all. :) With some search on the internet, I found this:

extending short url to source url using PHP and CURL - Hasin Hyder

, .

+4

CURL. , .

//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);
}
+1

Source: https://habr.com/ru/post/1741010/


All Articles