我尝试使用我的请求发送自定义标头,但guzzle发送默认标头。
这是我的代码中GuzzleClient
的创建
$httpClient = new \GuzzleHttp\Client( [
'headers' => [
['Accept' => ' text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'],
['Accept-Language' => 'en-US,en;q=0.5'],
['Upgrade-Insecure-Requests' => '1'],
['User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'],
['Accept-Encoding' => 'gzip, deflate, br']
]
] );
但我看到默认标题(用户代理:Guzzle)
我的代码有什么问题
似乎问题在于header数组的定义。在您的示例中,它由嵌套数组组成,但标题应定义为关联数组。
所以您的代码应该修改如下。
$httpClient = new \GuzzleHttp\Client( [
'headers' => [
'Accept' => ' text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language' => 'en-US,en;q=0.5',
'Upgrade-Insecure-Requests' => '1',
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36',
'Accept-Encoding' => 'gzip, deflate, br'
]
] );
这应该工作正常。希望这可以帮助。