尽管FOLLOWLOCATION使用cURL获得301 [英] Getting 301 with cURL despite FOLLOWLOCATION
问题描述
我收到301错误,尽管使用FOLLOWLOCATION和MAXREDIRS。
我不知道该怎么做,我尝试了一切我可以:HEADER到0,FOLLOWLOCATION到1,MAXREDIRS到30,改变USERAGENT多次,使用COOKIEFILE单独,然后COOKIEJAR与它,但没有。 p>
这里是最奇怪的部分:我试图刮除的同一个网站不给予301其他页面,只是为某些。任何想法
函数curl_start($ url){
$ ch = curl_init
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_USERAGENT,Mozilla / 5.0(Windows; U; Windows NT 6.0; en-US; rv:1.9.0.3)Gecko / 2008092417 Firefox / 3.0.4
curl_setopt($ ch,CURLOPT_REFERER,http://google.com/);
curl_setopt($ ch,CURLOPT_HEADER,1);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_ENCODING,gzip);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ ch,CURLOPT_FRESH_CONNECT,1);
$ html = curl_exec($ ch);
curl_close($ ch);
return $ html;
}
。所以我想它与 safe_mode
或者设置一个 open_basedir
有关。您可以通过使用 error_reporting(E_ALL);
启动脚本来检查输出警告。
I'm getting a 301 error, despite using FOLLOWLOCATION and MAXREDIRS. I have no idea what to do, I tried everything I could: HEADER to 0, FOLLOWLOCATION to 1, MAXREDIRS to 30, changed USERAGENT multiple times, used COOKIEFILE alone, then COOKIEJAR along with it, but nothing.
Here's the weirdest part: the same website I'm trying to scrape doesn't give 301 for other pages, just for certain ones. Any ideas??
function curl_start($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.4");
curl_setopt($ch, CURLOPT_REFERER, "http://google.com/");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
Your code works on my server. So I guess it has something to do with safe_mode
or an open_basedir
is set. You could check for an output warning by starting your script with error_reporting(E_ALL);
. It should show a warning.
Check the following link to see a solution.
http://php.net/manual/ro/function.curl-setopt.php#102121
这篇关于尽管FOLLOWLOCATION使用cURL获得301的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!