doujiao4705 2015-12-01 23:54
浏览 107
已采纳

Laravel Guzzle不起作用,但Curl确实如此

I'm using Guzzle for working with external API.

I'm using it this way:

$client = new Client;
$request = $client->request('GET', 'https://api.callrail.com/v1/companies.json', [
    'headers' => [
        'Authorization' => 'Token token="my_api_string"'
    ]
]);

return dd($request);

this is the output

Stream {#255 ▼
  -stream: stream resource @280 ▶}
  -size: null
  -seekable: true
  -readable: true
  -writable: true
  -uri: "php://temp"
  -customMetadata: []
}

but when I use just curl like this

$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);

the output looks as expected

{"page":1,"per_page":100,"total_pages":1,"total_records":11,
....
....

what am I doing wrong with the Guzzle?

  • 写回答

1条回答 默认 最新

  • dsbx40787736 2015-12-02 01:00
    关注

    You have set up the Guzzle request correctly, you just need to do more with the $request after you retrieve it.

    Add this line in after your request:

    $result = json_decode($request->getBody());

    Adding it in to your code it would look like this:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码