我需要集成一个API,所以我编写了以下函数:
public function test() {
$client = new GuzzleHttp\Client();
try {
$res = $client->post('http://example.co.uk/auth/token', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'json' => [
'cliend_id' => 'SOMEID',
'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
'grant_type' => 'client_credentials'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd($res);
}
catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
作为响应,我收到了这条消息:
{"data":{"error":"invalid_clientId","error_description":"ClientId should be sent."}}
现在当我尝试使用POSTMAN应用程序以相同的数据运行相同的URL时,我会得到正确的结果:
我的代码有什么问题?我发送了正确的form_params
,也尝试将form_params
更改为JSON,但仍然遇到相同的错误...
如何解决我的问题?
RequestOptions::FORM_PARAMS
常量而不是自己编写字符串中的数组键。 - WhySoToxic