我正在尝试连接 Google API 货币转换器。由于它返回无效的 json 响应,我不得不更改格式。但现在我被困在如何获取信息上。任何帮助将不胜感激。这是我的代码:
$amount = '100';
$from = 'USD';
$to = 'MXN';
// Make param to be sent in API
$string = $amount.$from."=?".$to;
// Call Google API
$google_url = "http://www.google.com/ig/calculator?hl=en&q=".$string;
// Get and Store API results into a variable
$result = file_get_contents($google_url);
$result = explode('"', $result);
$fromParts = explode(' ', $result[1]);
$toParts = explode(' ', $result[3]);
$return = array(
'from' => array(
'code' => $from,
'amount' => cleanAmount($fromParts[0])
),
'to' => array(
'code' => $to,
'amount' => cleanAmount($toParts[0])
)
);
$json = json_encode($return);
//echo json_encode($return);
我得到的回应是这样的:
{"from":{"code":"USD","amount":100},"to":{"code":"MXN","amount":1304.80167}}
我正在尝试提取 to->amount,但我得到一个空的结果。
$obj = json_decode($json);
print $obj->{'to'};
这是我得到的错误:
Catchable fatal error: Object of class stdClass could not be converted to string in /home/olympust/public_html/lista-traslados-mexico.php on line 101
有人知道如何从响应中获取变量吗?