【问题标题】:Laravel Guzzle doesn't work but Curl doesLaravel Guzzle 不起作用,但 Curl 可以
【发布时间】:2016-03-06 02:10:55
【问题描述】:

我正在使用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 做错了什么?

【问题讨论】:

    标签: php laravel curl guzzle


    【解决方案1】:

    您已经正确设置了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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      相关资源
      最近更新 更多