好吧,我使用coinbase的API来计算BTC的当前价格,但当我尝试使用json_decode()时,它会返回一个错误,这让我相信他们的响应不是json。
https://coinbase.com/api/v1/prices/spot_rate?currency=USD
返回:
{"amount":"90.00","currency":"USD"}
我尝试了json_decode($grabPrice);$grabPrice等于那个API的file_get_contets()。它给我的错误是:
Catchable fatal error: Object of class stdClass could not be converted to string in
如何获取PHP变量中的金额?
谢谢。
这是一个json编码的字符串。。。。
要从中获取数据,请首先使用json_decode
$data = json_decode($str);
echo $data->amount;
或者如果你更喜欢数组而不是对象
$data = json_decode($str, true);
echo $data["amount"];
此代码运行良好:-
$grab=file_get_contents('https://coinbase.com/api/v1/prices/spot_rate?currency=USD');
$resarray=json_decode($grab);
echo 'amount :'.$resarray->amount.'<br>';
echo 'currency :'.$resarray->currency;
输出:-
amount :89.91
currency :USD