Home  >  Article  >  Backend Development  >  PHP curl实现抓取302跳转后页面的示例_PHP

PHP curl实现抓取302跳转后页面的示例_PHP

WBOY
WBOYOriginal
2016-06-01 11:52:09805browse

PHP的CURL正常抓取页面程序如下:

$url = 'http://www.baidu.com';

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 20); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$ret = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch);


如果你抓取到的是302状态,是因为再抓取的过程中,有的跳转需要给下一个链接传递参数,而下一个链接同时也设置了如果没接收到相应的参数是为非法访问。

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');

显示就应该正常了。

上面用来抓取功能,几乎应该没问题的。你可以查一下CURLOPT_CUSTOMREQUEST相关资料。

使用一个自定义的请求信息来代替”GET”或”HEAD”作为HTTP请求。这对于执行”DELETE” 或者其他更隐蔽的HTTP请求。有效值如”GET”,”POST”,”CONNECT”等等。也就是说,不要在这里输入整个HTTP请求。例如输入”GET /index.html HTTP/1.0\r\n\r\n”是不正确的。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn