Curl, follow location but only get header of the new location?

前端 未结 7 1600
情书的邮戳
情书的邮戳 2020-12-19 13:56

I know that when I set CURLOPT_FOLLOWLOCATION to true, cURL will follow the Location header and redirect to new page. But is it possible only to get header of the new page w

相关标签:
7条回答
  • 2020-12-19 15:00

    Yes, you can set it to follow the redirect until you get the last location on the header response.

    The function to get the last redirect:

    function get_redirect_final_target($url)
    {
        $ch = curl_init($url);        
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow redirects
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // set referer on redirect
        curl_setopt($ch,CURLOPT_HEADER,false); // if you want to print the header response change false to true
        $response = curl_exec($ch);
        $target = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        curl_close($ch);
        if ($target)
            return $target; // the location you want
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题