PHP Curl得到403错误,但是来自同一台机器的浏览器可以请求页面吗? [英] PHP Curl gets 403 error, but browser from same machine can request page?
问题描述
我的脚本运行正常,没有任何问题。我一般地说,因为当它从CNN.com,allrecipes.com,reddit.com等检索页面时-当我将其指向至少一个URL(foxnews.com)时,我会收到403错误。
I've got this script working with generally no problems. I say generally, because while it retrieves pages from CNN.com, allrecipes.com, reddit.com, etc - when I point it towards at least one URL (foxnews.com), I get a 403 error instead.
如您所见,我已经将用户代理设置为与机器浏览器相同(这是通过向Facebook主页发送请求来完成的,该请求返回了一条消息,指出浏览器当时不是
As you can see, I've set the user agent to the same as my machine's browser (that was necessitated by sending a request to Facebook's homepage, which returned a message that the browser wasn't supported).
因此,基本上想知道要让尽可能多的网站将CURL请求识别为真实来源,我需要采取哪些步骤,
So, basically wondering what step(s) I need to take to have as many sites as possible recognize the CURL request as coming from a real, actual browser, rather than 403'ing it.
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
推荐答案
Fox News似乎阻止了对他们网站的访问任何通过 USERAGENT
的请求。只需删除 USERAGENT
字符串对我来说就可以正常运行:
Fox News appears to be blocking access to their website from any request passing a USERAGENT
. Simply removing the USERAGENT
string works fine for me:
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
希望这会有所帮助! :)
Hope this helps! :)
这篇关于PHP Curl得到403错误,但是来自同一台机器的浏览器可以请求页面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!