I am trying to connect with Google API currency converter. Since it returns an invalid json response, I had to change the format. But now I am stuck on how to get the information out. Any help would be greatly appreciated.
Here is my code:
$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);
The response I get is this:
{"from":{"code":"USD","amount":100},"to":{"code":"MXN","amount":1304.80167}}
And I am trying to extract the to->amount, but I am getting an empty result.
$obj = json_decode($json);
print $obj->{'to'};
And this is the error I am getting:
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
Anyone have any idea how to get the variables from response?
You are trying to
echoan object. You can do either:or