How to get live currency rates using PHP

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to get live currency rates using PHP

Post by Saman » Tue Dec 21, 2010 2:29 pm

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);
 
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to get live currency rates using PHP

Post by Saman » Tue Aug 02, 2011 9:19 pm

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

?>
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to get live currency rates using PHP

Post by Rksk » Tue Aug 02, 2011 10:18 pm

Thankz. REP+

[ Post made via Mobile Device ] Image
User avatar
Hikkurs
Posts: 2
Joined: Mon Dec 05, 2011 11:18 pm

Re: How to get live currency rates using PHP

Post by Hikkurs » Mon Dec 12, 2011 11:12 pm

Can I implement this code into my WP based blog? Where should I put it? TIA
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to get live currency rates using PHP

Post by Saman » Wed Dec 14, 2011 7:31 pm

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.
User avatar
asifqwq
Posts: 1
Joined: Wed Jan 04, 2012 11:53 am
Contact:

Re: How to get live currency rates using PHP

Post by asifqwq » Wed Jan 04, 2012 12:22 pm

A really nice idea. I appreciate it much !
Last edited by asifqwq on Tue Sep 01, 2020 1:18 pm, edited 1 time in total.
jaheen100
Corporal
Corporal
Posts: 9
Joined: Fri Jan 03, 2014 4:17 pm

Re: How to get live currency rates using PHP

Post by jaheen100 » Thu Jan 09, 2014 4:04 pm

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.
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to get live currency rates using PHP

Post by Saman » Fri Jan 10, 2014 11:57 am

Where is the php code? Programmers here needs that, not a site :)
Post Reply

Return to “PHP & MySQL”