我将调用以下url接口:https://api.gemini.com/v1/trades/btcusd?timestamp=1518710400&limit_trades=1
这将返回以下JSON:
[
{
"timestamp":1518710409,
"timestampms":1518710409004,
"tid":3051346543,
"price":"9837.17",
"amount":"0.00118501",
"exchange":"gemini",
"type":"sell"
}
]当我尝试从json字符串访问'price‘对象时,如下所示:
$response = $client->request('GET', 'https://api.gemini.com/v1/trades/btcusd?timestamp=1518710400&limit_trades=1');
$body = json_decode($response->getBody());var_dump()返回:
array(1) { [0]=> object(stdClass)#85 (7) { ["timestamp"]=> int(1518710409) ["timestampms"]=> float(1518710409004) ["tid"]=> float(3051346543) ["price"]=> string(7) "9837.17" ["amount"]=> string(10) "0.00118501" ["exchange"]=> string(6) "gemini" ["type"]=> string(4) "sell" } } 但是我得到了以下错误:
Notice: Trying to get property of non-object in (file path) on line 130130行是
echo $body->price为什么$body->price不是从JSON字符串返回的价格的有效访问器?
发布于 2018-02-16 09:51:26
$body是一个数组,因此您需要执行以下操作,因为它是该数组中的第一项:
$body[0]->price;https://stackoverflow.com/questions/48818882
复制相似问题