PHP,检查是否正在发送Guzzle标头?

时间:2017-07-24 05:48:26

标签: php guzzle

我对这个Guzzle有问题,或者我认为。 我的连接遭到拒绝,我认为缺少标题是原因。

这是我收到的错误:

Client error: `GET https://restapi.e-conomic.com/customers` resulted in a `401 Unauthorized` response:
{"message":"Unauthorized access (Unauthorized)."

我的代码:

$client = new GuzzleHttp\Client(['base_uri' => 'https://restapi.e-conomic.com/']);

$headers = [
    'X-AppSecretToken:demo',
    'X-AgreementGrantToken:demo',
    'Content-Type:application/json; charset=utf-8',
    'debug' => false
];

$response = $client->request('GET', 'customers', [
    'headers' => $headers
]);

你们知道如何检查,是否已发送标题,以及它们包含哪些内容?有没有办法验证令牌是否已提交?

此致

更新,这有效:

$headers = [
    'X-AppSecretToken' => 'demo',
    'X-AgreementGrantToken' => 'demo',
    'Content-Type' => 'application/json; charset=utf-8',
    'debug' => false
];

$response = $client->request('GET', 'customers', [
    'headers' => $headers
]);

return $response->getBody(); 

1 个答案:

答案 0 :(得分:2)

正如我在评论中提到的,headers是一个关联数组,所以:

[
    'Content-Type' => 'application/json; charset=utf-8'
]

[
    'Content-Type:application/json; charset=utf-8'
]

您可以阅读有关Guzzle PSR-7实施的更多信息(包括标题)here