[英]Guzzle 7 - 403 Forbidden (works fine with CURL)
UPDATE: it seems that the user-agent isn't the only header some hosts require to serve HTML, I also had to add the 'accepts' header, in the end this solved the problem for me with many hosts:更新:似乎用户代理不是唯一的 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'
]]);
I'm trying to use Guzzle to retrieve some websites but recieving a 403 forbidden error (when they work fine in a browser), I suspect this is down to non-standard User-Agents being forbidden by the host.我正在尝试使用 Guzzle 检索一些网站,但收到 403 禁止错误(当它们在浏览器中正常工作时),我怀疑这是由于主机禁止非标准用户代理。 To get around this, I am trying to set the User-Agent in Guzzle to mimic a browser but I can't find any method that actually works.
为了解决这个问题,我试图在 Guzzle 中设置 User-Agent 以模仿浏览器,但我找不到任何实际有效的方法。 I can browse to the website and also use WGET and CURL -L to download the HTML with no problems so the issue seems to be with Guzzle.
我可以浏览网站并使用 WGET 和 CURL -L 下载 HTML 没有问题,所以问题似乎出在 Guzzle 上。
I've tried:我试过了:
$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);
Weirdly this ^ one results in an error that seems to say Guzzle is trying to browse to the User-Agent value: cURL error 6: Could not resolve host: Mozilla (see https://curl.haxx.se/libcurl/c/libcurl-errors.html ) for Mozilla/5.0%20(Windows%20NT%206.2;%20WOW64)%20AppleWebKit/537.36%20(KHTML,%20like奇怪的是,这个 ^ 导致一个错误,似乎说 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);
^Results in a "Client error: GET http://www.xxx.co.uk
resulted in a `403 Forbidden'" error ^导致“客户端错误:
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);
^Results in a "Argument 3 passed to GuzzleHttp\Client::request() must be of the type array, string given" error ^导致“传递给 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);
^ Also results in a "Argument 3 passed to GuzzleHttp\Client::request() must be of the type array, string given" error ^ 还会导致“传递给 GuzzleHttp\Client::request() 的参数 3 必须是数组类型,给定字符串”错误
Any suggestions?有什么建议么? I've gone down a rabbit hole here I think!
我想我在这里掉了一个兔子洞!
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. 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.
UPDATE: it seems that the user-agent isn't the only header some hosts require to serve HTML, I also had to add the 'accepts' header, in the end this solved the problem for me with many hosts:更新:似乎用户代理不是唯一的 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.