[英]Guzzle 7 - 403 Forbidden (works fine with CURL)
更新:似乎用户代理不是唯一的 header 一些主机需要为 HTML 提供服务,我还必须添加“接受”Z099FB995346F31C749F6E40DB0F395EZ 解决了许多主机的问题:
$response = $client->request('GET', 'http://acme.com', ['headers' => ['user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
'accept'=> 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
]]);
我正在尝试使用 Guzzle 检索一些网站,但收到 403 禁止错误(当它们在浏览器中正常工作时),我怀疑这是由于主机禁止非标准用户代理。 为了解决这个问题,我试图在 Guzzle 中设置 User-Agent 以模仿浏览器,但我找不到任何实际有效的方法。 我可以浏览网站并使用 WGET 和 CURL -L 下载 HTML 没有问题,所以问题似乎出在 Guzzle 上。
我试过了:
$client = new Client(['allow_redirects' => ['track_redirects' => true]]);
$client->setUserAgent("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36");
$response = $client->get($domain_name);
奇怪的是,这个 ^ 导致一个错误,似乎说 Guzzle 正在尝试浏览到 User-Agent 值:cURL 错误 6:无法解析主机:Mozilla(请参阅https://curl.haxx.se/libcurl/c/ libcurl-errors.html ) Mozilla/5.0%20(Windows%20NT%206.2;%20WOW64)%20AppleWebKit/537.36%20(KHTML,%20like
$domain_name = 'http://www.' . $domain_name;
$client = new Client(['headers' => ['User-Agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36']]);
$response = $client->get($domain_name);
^导致“客户端错误: GET http://www.xxx.co.uk
导致‘403 Forbidden’”错误
$domain_name = 'http://www.' . $domain_name;
$client = new Client(['allow_redirects' => ['track_redirects' => true]]);
$client->setServerParameter('user-agent', "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36");
$response = $client->get($domain_name);
^导致“传递给 GuzzleHttp\Client::request() 的参数 3 必须是数组类型,给定字符串”错误
$domain_name = 'http://www.' . $domain_name;
$client = new Client(['allow_redirects' => ['track_redirects' => true]]);
$client->setHeader("user-agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36");
$response = $client->get($domain_name);
^ 还会导致“传递给 GuzzleHttp\Client::request() 的参数 3 必须是数组类型,给定字符串”错误
有什么建议么? 我想我在这里掉了一个兔子洞!
I'm wondering if something else is going on here because as I understand it, Guzzle is just a wrapper for CURL and CURL can fetch the same web page, from the same IP with no problem.
更新:似乎用户代理不是唯一的 header 一些主机需要为 HTML 提供服务,我还必须添加“接受”Z099FB995346F31C749F6E40DB0F395EZ 解决了许多主机的问题:
$response = $client->request('GET', 'http://acme.com', ['headers' => ['user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
'accept'=>'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9']]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.