Reputation: 1237
Say I want to get the response of this url:
http://www.google.com/ig/calculator?hl=en&q=100USD%3D%3FEUR
which is:
{lhs: "100 U.S. dollars",rhs: "79.3839803 Euros",error: "",icc: true}
And then retrieve the rhs value.
How can i achieve this easily in PHP?
Upvotes: 0
Views: 113
Reputation: 1797
It is not returning valid JSON, so the easy way is to fix the string (to make it legal JSON) and decode it.
Something like:
<?php
$output = file_get_contents('http://www.google.com/ig/calculator?hl=en&q=100USD%3D%3FEUR');
$output = str_replace('rhs','"rhs"',$output);
$output = str_replace('lhs','"lhs"',$output);
$output = str_replace('error','"error"',$output);
$output = str_replace('icc','"icc"',$output);
$json = json_decode($output);
$rhs = $json->rhs;
?>
Upvotes: 1
Reputation: 3028
<?php
$resp=file_get_contents('http://www.google.com/ig/calculator?hl=en&q=100USD%3D%3FEUR');
$pos=strpos($resp,'rhs:')+6;
$pos2=strpos($resp,'"',$pos);
$euros=substr($resp,$pos,$pos2-$pos);
?>
Upvotes: 0
Reputation: 83652
echo file_get_contents('http://www.google.com/ig/calculator?hl=en&q=100USD%3D%3FEUR');
// {lhs: "100 U.S. dollars",rhs: "79.3839803 Euros",error: "",icc: true}
Upvotes: 1