Reputation: 281
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.
Upvotes: 0
Views: 6748
Reputation: 288090
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.
Upvotes: 2
Reputation: 736
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>
Upvotes: 0