Laravel Guzzle 不起作用,但 Curl 起作用
Laravel Guzzle doesn't work but Curl does
我正在使用 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: []
}
但是当我像这样只使用 curl
$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);
我正在使用 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: []
}
但是当我像这样只使用 curl
$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);