【问题标题】:Laravel guzzle get responseLaravel guzzle 得到响应
【发布时间】:2020-09-28 05:29:53
【问题描述】:

我正在使用 laravel guzzle 包从这个 https://eos.greymass.com/v1/history/get_transaction url 获取响应

    $client = new Client();
    try {
        $response = $client->request('GET', 'https://eos.greymass.com/v1/history/get_transaction?id=18a20dbc34082451143c03ac54a2f24d06494d51e68f8fb1211ca0b63a53f37d');
    }catch (ClientException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        return redirect()->back()->with('error', $responseBodyAsString);
    }
    if ($response->getStatusCode() != 200){
        return redirect()->back()->with('error', 'Status code must be 200');
    }

    $body = $response->getBody();
    return $body;

我正确获取了 $body 数据,但是当我尝试获取 $body->block_num 时,然后向我显示了这个未定义的属性:GuzzleHttp\Psr7\Stream::$block_num 错误

【问题讨论】:

  • 你试过调试你的 $body 吗?您的代码根本不显示 $body->block_num。

标签: php laravel api guzzle6


【解决方案1】:

你必须解码 $response 才能得到它,因为它会将 json 转换为一个对象 比如:

$response = json_decode($client->request('GET', 'https://eos.greymass.com/v1/history/get_transaction?id=18a20dbc34082451143c03ac54a2f24d06494d51e68f8fb1211ca0b63a53f37d')->getBody(), true);

试试这个!它会帮助你

【讨论】:

  • 你也可以直接使用$client->request('GET', '...')->json(),不是吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 2017-10-24
  • 1970-01-01
  • 2019-08-04
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多