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 错误。

如您所见,我已将用户代理设置为与我机器的浏览器相同(这是通过向 Facebook 主页发送请求所必需的,该主页返回了一条消息,表明浏览器不受支持)。

所以,基本上想知道我需要采取哪些步骤才能让尽可能多的站点将 CURL 请求识别为来自真实的实际浏览器,而不是 403 请求。

    $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 字符串对我来说很好:

$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);

希望对您有所帮助! :)