【发布时间】:2018-10-09 04:03:27
【问题描述】:
我需要访问一个 API,所以我使用 guzzle6 并编写了一个函数:
public function test()
{
$client = new GuzzleHttp\Client(['defaults' => ['verify' => false]]);
try {
$res = $client->post('https://example.com/api/v2/oauth/token?grant_type=client_credentials', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'auth' => [
'Username' => 'user_5639',
'Password' => 'pass_asdhbas67yausihd7qaw8'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
}
catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
但我得到了错误:
Client.php 第 346 行中的 ErrorException:未定义偏移量:0
如何解决我的问题?
【问题讨论】:
标签: api laravel-5.1 guzzle