Simon Davies Simon Davies - 5 months ago 25
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 = 'http://www.google.com/ig/calculator?hl=en&q=' . $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);
curl_close($ch);

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


BUT having looked they are using a different URL:

'http://www.google.com/finance/converter?hl=en&a=' . $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

http://www.w3.org/TR/html4/strict.dtd


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

Answer

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("https://www.google.com/finance/converter?a=$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]);