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(); 
马特乌斯啜饮

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

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

不是:

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

您可以在此处阅读有关 Guzzle 的 PSR-7 实现(包括标头)的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章