PHP:cURL并跟踪所有重定向
我希望卷曲一个URL,并跟踪它通过的每个URL。出于某种原因,如果不进行递归cURL调用,我就无法实现这一点,这是不理想的。也许我错过了一些简单的选择。想法PHP:cURL并跟踪所有重定向,php,curl,libcurl,Php,Curl,Libcurl,我希望卷曲一个URL,并跟踪它通过的每个URL。出于某种原因,如果不进行递归cURL调用,我就无法实现这一点,这是不理想的。也许我错过了一些简单的选择。想法 $url = "some url with redirects"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch,
$url = "some url with redirects";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");
$html = curl_exec($ch);
$info = array();
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
echo "<pre>";
print_r($info);
echo "</pre>";
}
$url=“带有重定向的某些url”;
$ch=curl\u init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_头,true);
curl_setopt($ch,CURLOPT_NOBODY,false);
curl_setopt($ch,CURLOPT_超时,10);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch,CURLOPT_USERAGENT,“Mozilla/5.0(Macintosh;U;英特尔Mac OS X;en-US;rv:1.8.1)Gecko/20061024 BonEcho/2.0”);
$html=curl\u exec($ch);
$info=array();
如果(!curl_errno($ch))
{
$info=curl\u getinfo($ch);
回声“;
打印(信息);
回声“;
}
我得到了这样的回应
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
数组
(
[url]=>命中的最后一个url
[content\u type]=>text/html;字符集=utf-8
[http_代码]=>200
[标题大小]=>1942
[请求大小]=>1047
[filetime]=>-1
[ssl\u验证\u结果]=>0
[重定向\u计数]=>20.799589
[namelookup_time]=>0.000741
[连接时间]=>0.104206
[预传输时间]=>0.104306
[上传大小]=>0
[下载大小]=>49460
[速度下载]=>61856
[速度上传]=>0
[下载内容长度]=>49460
[上传内容长度]=>0
[开始传输时间]=>0.280781
[重定向时间]=>0.400723
)
您有
function getWebPage($url, $redirectcallback = null){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");
$html = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
list($httpheader) = explode("\r\n\r\n", $html, 2);
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $httpheader, $matches);
$nurl = trim(array_pop($matches));
$url_parsed = parse_url($nurl);
if (isset($url_parsed)) {
if($redirectcallback){ // callback
$redirectcallback($nurl, $url);
}
$html = getWebPage($nurl, $redirectcallback);
}
}
return $html;
}
function trackAllLocations($newUrl, $currentUrl){
echo $currentUrl.' ---> '.$newUrl."\r\n";
}
getWebPage('some url with redirects', 'trackAllLocations');
这意味着cURL将遵循重定向,只返回没有位置标题的最后一页
要手动跟踪位置,请执行以下操作:
preg_match('/(Location:|URI:)(.*?)\n/', $httpheader, $matches);
我可以推荐一下
$url = curl_getinfo($ch, CURLINFO_REDIRECT_URL)
将正则表达式更改为/(位置:| URI:)(.*)\n/i,使其不区分大小写。我注意到有些站点/地方使用location:其中L是小写
只是想帮助那些想知道为什么有时候它不起作用的人。。。看看这个。对于libcurl,您可以使用getinfo变量来查找它在启用时会重定向到的URL。这使程序能够轻松地遍历重定向本身 这种方法比其他人在这里建议的解析
Location:
头好得多,也更容易,因为这样您的代码就必须重建相对路径等。CURLINFO\u REDIRECT\u URL
会自动为您修复这些问题
PHP 5.3.7中添加的PHP/CURL绑定:
解决此问题的提交:
嘿,链接不再起作用了。这已经实现了吗?如果是的话,你知道如何从PHP中使用它吗?我很感谢这一见解,我更喜欢它,而不是手动循环状态代码和查找重定向URL。例如,
$redirectcallback
,对我来说不起作用
$url = curl_getinfo($ch, CURLINFO_REDIRECT_URL)