PHP and curl for fetching currency rate from Yahoo Finance
Posted
by gregj
on Stack Overflow
See other posts from Stack Overflow
or by gregj
Published on 2010-05-15T22:41:30Z
Indexed on
2010/05/15
22:50 UTC
Read the original article
Hit count: 348
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."&from=".$from."&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.
© Stack Overflow or respective owner