Simon Davies Simon Davies - 7 months ago 47
PHP Question

Google Currency Converter has changed its URL but not getting same result

I have the following code (below) and was using the iGoogle version.

$url = '' . $amount . $from_Currency . '=?' . $to_Currency;

$ch = curl_init();
$timeout = 0;

curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);

$data = explode('"', $rawdata);
$data = explode(' ', $data[3]);
$var = $data[0];

BUT having looked they are using a different URL:

'' . $amount . '&from=' . $from_Currency . '&to=USD';

But simply changing the url does not return the required result that i was used to.

Now all i would get is

SO has anyone worked on this latest currency converter URL or have any ideas. Or a replacement using PHP


Thanks to some more in depth looking and rewording of issue found this post. So in a way its a duplicate. but here is the question:

Need API for currency converting

I used @hobailey answer for a temporary fix until i can update it to another version or google decide to do a proper api.

  $amount = urlencode($amount);
  $from_Currency = urlencode($from_Currency);
  $to_Currency = urlencode($to_Currency);
  $get = file_get_contents("$amount&from=$from_Currency&to=$to_Currency");
  $get = explode("<span class=bld>",$get);
  $get = explode("</span>",$get[1]);  
  $converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);