簡體   English   中英

如何在PHP中獲取302重定向URL的Location頭?

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

我試圖找到一種通用的方法來擴展大多數(如果不是全部)縮短的URL。 我知道bit.ly,TinyURL,goo.gl等短網址使用302重定向方法將您重定向到另一個網站。 如何在php中對縮短的URL發出HEAD請求並獲取標題的“Location”部分?

請幫我解決一下這個。

謝謝

忘了每個人。 :)通過一些互聯網搜索,我發現了這個:

使用PHP和CURL將短網址擴展到原始網址 - Hasin Hayder

它告訴我如何做到這一點。

你需要使用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM