1

I have a problem with my PHP Script. It works fine if i convert less than 20 USD to INR but if i used it with more than 19 ($amount) then it shows only 1st digit of the resultant output. I want full resultant output to use this script. I am feeling glad if someone assist me to solve this problem.

Here's the code:

function currency($from_Currency,$to_Currency,$amount) {
    $amount = urlencode($amount);
    $from_Currency = urlencode($from_Currency);
    $to_Currency = urlencode($to_Currency);
    $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'];
    return round($var,3);
}

$from_Currency ="USD";
$to_Currency ="INR";
$amount ="20";

echo currency($from_Currency,$to_Currency,$amount);
5
  • you'd better use an official api to do such a thing, like Yahoo Finances API. – Alain Tiemblo Oct 31 '12 at 7:27
  • Use ECB XML: stackoverflow.com/a/7125996/584490 BTW, this question is a duplicate of too many other questions. – Tiberiu-Ionuț Stan Oct 31 '12 at 7:34
  • Try this also: stackoverflow.com/a/2461842/584490 – Tiberiu-Ionuț Stan Oct 31 '12 at 7:35
  • @Tiberiu-IonuțStan i know that this is duplicate question and asked many time at stackoverflow, i try many times to find a suitable answer for my query and after it i open a new question here. And your given link is not suitable for my query. Thanks for the help. – Rohit Pawar Oct 31 '12 at 7:51
  • Your solution is not suitable to the problem. – Tiberiu-Ionuț Stan Oct 31 '12 at 8:49
2

try this

$data = explode('"', $rawdata);
preg_match_all("/([0-9]+(\.[0-9]+)?\s?[kBM]?)/", $data[3], $match1);
$var = implode(",", $match1[0]);
$var = str_replace(",", "", $var);
return round($var, 3);
3
  • yeah, that's it! thank you @myron for such a nice help! It works, but what should i do to not show last 4 digit of output i.e, 20 USD to INR output is 1,077,00592 and we don't want to show 00592. The last values is not much necessary. – Rohit Pawar Oct 31 '12 at 8:30
  • maybe you could try the edited code above instead. hope you find this answer helpful. cheers! – Lao Oct 31 '12 at 8:40
  • cool, everything seems to be fine! thanks once again @myron sir to make my web project a success. – Rohit Pawar Oct 31 '12 at 8:48
0

That is because google seems to be returning a weird number in case of > 1000 rhs value. Print the whole response and you will see. The value returned is like 969 for $18, and 1á082.30965 for $20. I do not recognize that extra character in there, and no documentation is available.

Use a work around - convert 1 unit and then multiply with amount.

1
  • if it is possible for me to multiply every amount of my website then i definitely multiply the query amount manually. – Rohit Pawar Oct 31 '12 at 7:54
0

I modified your code. This works as shown below the function...

function currency($from_Currency,$to_Currency,$amount) {
    $amount = urlencode($amount);
    $from_Currency = urlencode($from_Currency);
    $to_Currency = urlencode($to_Currency);
    $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 = preg_replace("/((\"?[^\"]+\"?)[ ]*:[ ]*([^,\"]+|\"[^\"]*\")(,?))/i", '"\\2": \\3\\4', str_replace(array('{', '}'), array('',''), $rawdata));
    return json_decode( '{' . $data . '}' );
}

$from_Currency ="USD";
$to_Currency ="INR";
$amount ="20";

$amt = currency($from_Currency,$to_Currency,$amount);
//print_r($amt);

// The actual converted amount can be obtained with
$converted_amount = $amt->rhs;
5
  • thanks for the help, but your code doesn't execute, it show blank page. – Rohit Pawar Oct 31 '12 at 7:45
  • ah, the preg_replace left off the enclosing {} so the json_decode didn't work. Fixed the function to return the decoded object. – Pastor Bones Oct 31 '12 at 7:57
  • I deleted all the crud and explanation...there is your working code. – Pastor Bones Oct 31 '12 at 8:05
  • thanks sir but it again show a blank page, don't got any output. – Rohit Pawar Oct 31 '12 at 8:12
  • It works as shown here. Nothing is being output in my example...add echo $converted_amount; at the end to output the amount – Pastor Bones Oct 31 '12 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.