Reputation: 3267
I am trying to convert currency data using Google API and PHP curl. unfortunately i am having the following problem and am not able to solve it still. can any one help me please. here is my PHP 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 = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return round($var,2);
}
// here is my function call
$usd= currency("USD","ETB",1);
echo "270 USD= ".$usd."ETB";
but i am having the follwing error
Notice: Undefined offset: 3 ... on line .... (14 of this function)
Upvotes: 0
Views: 3395
Reputation: 158
It seems your API URL is wrong because when $rawdata
echo-ed, Google returning Error 404. For now, the available solution (if you're insist to use Google) is to convert currency using Google Finance. For example, using GET
request, you can send a request with the following format:
$url = "https://www.google.com/finance/converter?a=" . $amount . "&from=" . $from . "&to=" . $to;
Unfortunately, Google return a full page HTML, so you need to parse the result manually. Just now I'm trying to access it using https://www.google.com/finance/converter?a=1&from=IDR&to=USD, and the conversion result is attached in a <div>
like this:
<div id=currency_converter_result>1 IDR = <span class=bld>0.0001 USD</span>
So, if you save the result in a $rawdata
variable, you can use regular expression in PHP to obtain the conversion result. Since it's not a formal API, you need to actively watching the page structure next time if the code doesn't work.
This is your code, updated with a test:
function convertCurrency($amount, $from_Currency, $to_Currency) {
$url = "https://www.google.com/finance/converter?a=" . $amount . "&from=" . $from_Currency . "&to=" . $to_Currency;
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$rawdata = curl_exec($ch);
curl_close($ch);
preg_match("/<span class=bld>(.*)<\/span>/", $rawdata, $converted);
$converted = preg_replace("/[^0-9.]/", "", $converted);
return round($converted[0], 3);
}
$usd = currency("USD", "ETB", 270);
echo "270 USD = " . $usd . " ETB";
My suggestion is: find another API for currency conversion. You can look for alternative like https://openexchangerates.org/. Unfortunately, it's a paid service.
Upvotes: 2
Reputation: 2935
Google has changed its url to
https://www.google.com/finance/converter?a
Try like this
function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$get = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency");
$get = explode("<span class=bld>",$get);
$get = explode("</span>",$get[1]);
$converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);
return round($converted_amount,2);
}
echo currency("USD","INR",2);
Upvotes: 0