PHP-curl一个有302跳转的页面,返回403错误?

PHP-curl一个有302跳转的页面,返回403错误?

虐人心 发布于 2017-07-21 字数 459 浏览 1143 回复 2

代码如下:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
$ret = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

print_r($info);

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2017-09-03 2 楼

curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

CURLOPT_FOLLOWLOCATION //是否抓取跳转后的页面

浮生未歇 2017-07-29 1 楼

我之前也遇到过这问题,是因为再抓取的过程中,有的跳转需要给下一个链接传递参数,而下一个链接同时也设置了如果没接收到相应的参数是为非法访问。标准的抓取curl:

$url = 'http://www.domain.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);

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

 A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request. This is useful for doing "DELETE" or other, more obscure HTTP requests. Valid values are things like "GET", "POST", "CONNECT" and so on; i.e. Do not enter a whole HTTP request line here. For instance, entering "GET /index.html HTTP/1.0rnrn" would be incorrect.
Note: Don't do this without making sure the server supports the custom request method first.