获取CURLOPT_FOLLOWLOCATION重定向URL列表

如何在加载CURLOPT_FOLLOWLOCATION设置为True的页面时获取CURL所遵循的URL列表?</ p>

我知道有一个curl_getinfo($ ch,CURLINFO_EFFECTIVE_URL)函数,但只返回最后一个URL。</ p>
</ div>

展开原文

原文

How can I get a list of URLs that CURL follows when loading a page with a CURLOPT_FOLLOWLOCATION set to True?

I know there is a curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) function, but that returns just the last URL.

2个回答



以下代码应该适用于您,它使用 CURLOPT_HEADERFUNCTION </ code> cURL选项:</ p>

  $ locations = array(); 

$ curl = curl_init($ url);
curl_setopt($ curl,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ curl,CURLOPT_HEADERFUNCTION,function($ curl,&amp; $ header)使用(&amp; $ locations){
$ key ='Location:';
if(strpos($ header,$ key)=== 0){
$ locations [] = trim(substr($ header,strlen($ key)));
}
返回strlen($ header);
});
// ...
ncurl_exec($ curl);
</ 代码> </ pre>

$ locations </ code>将包含cURL重定向到的URL。</ p>
</ div>

展开原文

原文

The following code should work for you, which uses the CURLOPT_HEADERFUNCTION cURL option:

$locations = array();

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADERFUNCTION, function($curl, &$header) use(&$locations) {
    $key = 'Location:';
    if (strpos($header, $key) === 0) {
        $locations[] = trim(substr($header, strlen($key)));
    }
    return strlen($header);
});
// ...
curl_exec($curl);

$locations will contain the URLs that cURL was redirected to.



您应该能够使用 CURLOPT_HEADER </ code>选项并检查返回的标头。 不幸的是,这需要解析出位置标题,因为我不知道从返回的内容中获取特定标题值的更直接方法。</ p>

您可能同样适合 关闭重定向选项并手动跟进cURL请求以遵循重定向,记录重定向信息。</ p>
</ div>

展开原文

原文

You should be able to use CURLOPT_HEADER options and inspect the headers that were returned. Unfortunately this would require parsing out the location headers, as I don't know of a more direct way of getting the specific headers values out of returned content.

You might be just as well suited to turn off the option to follow redirects and manually make follow up cURL requests to follow the redirections, logging the redirection information along the way.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问