当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP curl实现抓取302跳转后页面的示例

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

2019年03月21日  | 移动技术网IT编程  | 我要评论

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 / http/1.0\r\n\r\n”是不正确的。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网