0
votes

i want to convert currency from USD to INR, the value in USD retrive from url and i want to convert it in INR according the current rate. here is the first code:

<?php 
 require_once('currency.php');

 $val=$_GET["val"];

 echo currency($val);

 ?>

and the second code is:

<?php

function currency($val) {
$amount = $val;

 $url = "http://www.google.com/ig/calculator?hl=en&q=$amountUSD=?INR";
 $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);
 }

 ?>

bytheway am testing this code on 0fees.net, the free hosting site, so is there any problem for that as am trying live USD to INR conversion.

2
ANd? Is there a question here?Mark Baker
it is not working. can any one give me a proper solution?dhruba
check my answer... i have solved this....Adeel Mughal
check my comment on my answer.... and one thing more, on your php code have bug that i have solved out...Adeel Mughal

2 Answers

2
votes

The error lies in the following code:

function currency($val) {
    $amount = $val;
    $url = "http://www.google.com/ig/calculator?hl=en&q=$amountUSD=?INR";
    // ...                                              ^^^^^^^^^^
}

php tries to evaluate the variable $amountUSD (according to php's string parsing rules) and fails with a notice:

PHP Notice:  Undefined variable: amountUSD in usdtoinr.php code on line 3

Instead, you should write:

function currency($val) {
    $amount = floatval($val);
    $url = 'http://www.google.com/ig/calculator?hl=en&q=' . $amount . 'USD=?INR';
    // ...
}

To catch these errors in the future, make sure to set error_reporting to E_ALL | E_STRICT on your development machine.

Also, the result of google's query is a JSON document. Since the order of properties in the objects may vary, you must use a proper JSON parser such as json_decode to parse it, like this:

$data = json_decode($rawdata, true);
$tmp = explode(' ', $data['rhs']);
return floatval($tmp[0]);

In general, it is also a good idea to include a hint to your actual user agent (for example the homepage of your software) in the user agent.

0
votes

Use this PHP code

<?php

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);
}

?>

and Use this for output, when you enter any amount it will be converted from USD to INR

<?php 
 require_once('currency.php');

  $amount=@$_GET["val"];

$from='USD';

$to='INR';

 echo currency($from,$to,$amount);

 ?>

<form method="get" ><input name="val" type="text"><input type="submit" value="Submit"></form>