views:

182

answers:

1

Hello,

I wrote the following php snippet to fetch the currency conversion rate from Yahoo Finance.Im using curl to fetch the data. Suppose, i want to convert from US dollars (USD) to Indian National Rupee (INR),then the url is http://in.finance.yahoo.com/currency/convert?amt=1&from=USD&to=INR&submit= and the Indian Rupee value is shown as 45.225. However,if i run my code, the value im getting is 452.25. Why this discrepancy ?

<?php

  $amount = $_GET['amount'];
  $from = $_GET['from'];
  $to = $_GET['to']; 
  $url = "http://in.finance.yahoo.com/currency/convert?amt=".$amount."&amp;from=".$from."&amp;to=".$to;
  $handle = curl_init($url);
  curl_setopt ($handle, CURLOPT_RETURNTRANSFER, true);
  $data = curl_exec($handle);
  if(preg_match_all('/<td class="yfnc_tabledata1"><b>(?:[1-9]\d+|\d)(?:\.\d\d)?/',$data,$matches))
  {
    print_r($matches[0][1]);
  }
  else
  {
    echo "Not found !";
  }
  curl_close($handle);

?>

Is there something wrong with my regex ?

Please help. Thank You.

+1  A: 

Yahoo Finance (almost) certainly has a corresponding API, so you don't have to parse some random HTML for currency conversion.

Also, I would presume using something like Google's http://www.google.com/ig/calculator?q=1 EUR IN USD and parsing this response is much more stable than parsing Yahoo's HTML page.

Joel L