Page 1 of 1

How to get live currency rates using PHP

Posted: Tue Dec 21, 2010 2:29 pm
by Saman
In this method, we used to get live currency rates from Google. This function uses curl so make sure you have enabled curl extension in php.

Code: Select all

<?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?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,4);
}

?>
Usage:

Code: Select all

echo currency('GBP', 'USD', 1);
 

Re: How to get live currency rates using PHP

Posted: Tue Aug 02, 2011 9:19 pm
by Saman
Google has changed the protocol. Here is the new code.

Code: Select all

<?php

function get_currency($from_Currency, $to_Currency, $amount) {

	$amount = urlencode($amount);
	$from_Currency = urlencode($from_Currency);
	$to_Currency = urlencode($to_Currency);
	$url = "http://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_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('bld>', $rawdata);
	$data = explode($to_Currency, $data[1]);

	return round($data[0], 2);
}

?>

Re: How to get live currency rates using PHP

Posted: Tue Aug 02, 2011 10:18 pm
by Rksk
Thankz. REP+

[ Post made via Mobile Device ] Image

Re: How to get live currency rates using PHP

Posted: Mon Dec 12, 2011 11:12 pm
by Hikkurs
Can I implement this code into my WP based blog? Where should I put it? TIA

Re: How to get live currency rates using PHP

Posted: Wed Dec 14, 2011 7:31 pm
by Saman
Sorry for my delayed reply.

Yes you can add this to any php based code. I can't tell you where exactly you need to put that since it is solely depend on your requirement. If you don't know php, you can get someone to work it out for you.

Re: How to get live currency rates using PHP

Posted: Wed Jan 04, 2012 12:22 pm
by asifqwq
A really nice idea. I appreciate it much !

Re: How to get live currency rates using PHP

Posted: Thu Jan 09, 2014 4:04 pm
by jaheen100
You can get easily live currency rates at http://www.forextrading.pk/open-market- ... -rates.php . This site will help You to check currency rates and give other information about the open market.

Re: How to get live currency rates using PHP

Posted: Fri Jan 10, 2014 11:57 am
by Saman
Where is the php code? Programmers here needs that, not a site :)