狂饮不发送自定义标题

时间:2019-03-09 20:16:46

标签: php http 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)

我的代码有什么问题

1 个答案:

答案 0 :(得分:2)

似乎问题出在标头数组的定义上。在您的示例中,它由嵌套数组组成,但是标头应定义为关联数组。

因此,您的代码应进行如下修改。

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

这应该工作正常。 希望这会有所帮助。