这是我在浏览器中直接访问$url时得到的输出。
{"message": "成功", "result":{"金额":0.43, "总币数":4306, "货币类型": "Rs"}, "return_code":200}。
当我试图在我的php代码中以 "余额是$amount "的方式访问和打印金额时,我得到的输出是0。 可能是什么原因造成的错误?
<?php
$url="http://localhost/account?id=c36571c7b2814c338b24c4c6e51098f1&secret_id=98bb36d4b4c1360ebd9a7386c74cbdb6&user_id=Tsa78/";
$jsonData = file_get_contents($url);
$arr = json_decode($jsonData,true);
$amount = $arr["result"]["amount"];``
echo "Balance is ". $amount;
?>
你需要做的第一步是将JSON数据转换为一个php数组。
第二步是从php数组中选择数据。
最后一步是打印数据(为了简单起见,用echo语句来完成).
下面的代码将打印金额。
<?php
$jsonData = '{"message":"success","result":{"amount":0.43,"total_coins":4306,"currency_type":"Rs"},"return_code":200}';
$phpArray = json_decode($jsonData, true);
$amount = $phpArray["result"]["amount"];
echo "Amount is ". $amount;
?>