我正在使用Guzzle来处理外部API.
我这样使用它:
$client = new Client; $request = $client->request('GET', 'https://api.callrail.com/v1/companies.json', [ 'headers' => [ 'Authorization' => 'Token token="my_api_string"' ] ]); return dd($request);
这是输出
Stream {#255 ? -stream: stream resource @280 ?} -size: null -seekable: true -readable: true -writable: true -uri: "php://temp" -customMetadata: [] }
但是当我像这样使用卷曲时
$api_key = 'my_api_string'; $ch = curl_init($api_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Token token=\"{$api_key}\"")); $json_data = curl_exec($ch); return dd($json_data);
输出看起来像预期的那样
{"page":1,"per_page":100,"total_pages":1,"total_records":11, .... ....
我对Guzzle做错了什么?
您已Guzzle
正确设置请求,只需$request
在检索后执行更多操作即可.
在您的请求后添加此行:
$result = json_decode($request->getBody());
将其添加到您的代码中它将如下所示:
$client = new Client; $request = $client->request('GET', 'https://api.callrail.com/v1/companies.json', [ 'headers' => [ 'Authorization' => 'Token token="my_api_string"' ] ]); $result = json_decode($request->getBody()); return dd($result);