![](/img/trans.png)
[英]Header response “403 Forbidden” with cURL, file_get_contents() and fopen()
[英]Why is using cURL giving me a 403 forbidden error when file_get_contents works?
这是我的PHP代码:
$ch = curl_init('http://www.iphonehacks.com/2016/08/jailbreak-iphone-ipad-ios-9-3-3-pangu-1-1.html');
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
上面的代码给出了403错误,但是当我使用file_get_contents
,我可以回显整个网页。
$data = file_get_contents('http://www.iphonehacks.com/2016/08/jailbreak-iphone-ipad-ios-9-3-3-pangu-1-1.html');
echo $data;
任何人都可以指出cURL方法有什么问题。 我知道我可以使用file_get_contents
但我有兴趣了解第一种情况下出了什么问题,以便更好地理解cURL。
您的CURLOPT_USERAGENT
选项不正确。 将其更改为:
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
您不应在CURLOPT_USERAGENT
选项上使用前缀“User-Agent:”。 该网站拒绝了它,因为它收到了以下请求标题:
User-Agent:User-Agent:Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,类似Gecko)Chrome / 51.0.2704.103 Safari / 537.36
代替:
User-Agent:Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,类似Gecko)Chrome / 51.0.2704.103 Safari / 537.36
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.