How to get live currency rates using PHP

How to get live currency rates using PHP

Postby Saman » Tue Dec 21, 2010 8:59 am

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.

Syntax: [ Download ] [ Hide ]
Using PHP Syntax Highlighting
  1. <?php
  2.  
  3. function currency($from_Currency, $to_Currency, $amount) {
  4.     $amount = urlencode($amount);
  5.     $from_Currency = urlencode($from_Currency);
  6.     $to_Currency = urlencode($to_Currency);
  7.  
  8.     $url = "http://www.google.com/ig/calculator?q=$amount$from_Currency=?$to_Currency";
  9.     $ch = curl_init();
  10.     $timeout = 0;
  11.     curl_setopt ($ch, CURLOPT_URL, $url);
  12.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  13.     curl_setopt ($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  14.     curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  15.     $rawdata = curl_exec($ch);
  16.     curl_close($ch);
  17.  
  18.     $data = explode('"', $rawdata);
  19.     $data = explode(' ', $data[3]);
  20.     $var = $data[0];
  21.     return round($var,4);
  22. }
  23.  
  24. ?>
Syntax parsed in 0.058 seconds


Usage:
Syntax: [ Download ] [ Hide ]
Using PHP Syntax Highlighting
  1. echo currency('GBP', 'USD', 1);
  2.  
Syntax parsed in 0.051 seconds
User avatar
Saman
Support Team
Support Team
 
Posts: 812
Joined: Fri Jul 31, 2009 5:02 pm
Cash on hand: 152,553.20
Location: Mount Lavinia
Medals: 1
EC_Achievment (1)

Invitations sent: 44
Registered friends: 1
Reputation point: 37
Staff Sergeant

Re: How to get live currency rates using PHP

Postby Saman » Tue Aug 02, 2011 3:49 pm

Google has changed the protocol. Here is the new code.

Syntax: [ Download ] [ Hide ]
Using PHP Syntax Highlighting
  1. <?php
  2.  
  3. function get_currency($from_Currency, $to_Currency, $amount) {
  4.  
  5.         $amount = urlencode($amount);
  6.         $from_Currency = urlencode($from_Currency);
  7.         $to_Currency = urlencode($to_Currency);
  8.         $url = "http://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency";
  9.         $ch = curl_init();
  10.         $timeout = 0;
  11.         curl_setopt ($ch, CURLOPT_URL, $url);
  12.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  13.         curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  14.         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  15.         $rawdata = curl_exec($ch);
  16.         curl_close($ch);
  17.        
  18.         $data = explode('bld>', $rawdata);
  19.         $data = explode($to_Currency, $data[1]);
  20.  
  21.         return round($data[0], 2);
  22. }
  23.  
  24. ?>
  25.  
Syntax parsed in 0.056 seconds
User avatar
Saman
Support Team
Support Team
 
Posts: 812
Joined: Fri Jul 31, 2009 5:02 pm
Cash on hand: 152,553.20
Location: Mount Lavinia
Medals: 1
EC_Achievment (1)

Invitations sent: 44
Registered friends: 1
Reputation point: 37
Staff Sergeant

Re: How to get live currency rates using PHP

Postby Rksk » Tue Aug 02, 2011 4:48 pm

Thankz. REP+

[ Post made via Mobile Device ] Image
User avatar
Rksk
 
Posts: 751
Joined: Thu Jan 07, 2010 10:49 am
Cash on hand: 4,834.60
Bank: 212,150.20
Location: Rathnapura, Sri Lanka
Medals: 1
EC_Achievment (1)

Invitations sent: 35
Registered friends: 12
Reputation point: 12
Corporal

Re: How to get live currency rates using PHP

Postby Hikkurs » Mon Dec 12, 2011 5:42 pm

Can I implement this code into my WP based blog? Where should I put it? TIA
User avatar
Hikkurs
 
Posts: 2
Joined: Mon Dec 05, 2011 5:48 pm
Cash on hand: 153.60

Invitations sent: 0
Registered friends: 0
Reputation point: 0
Lance Corporal

Re: How to get live currency rates using PHP

Postby Saman » Wed Dec 14, 2011 2:01 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
Saman
Support Team
Support Team
 
Posts: 812
Joined: Fri Jul 31, 2009 5:02 pm
Cash on hand: 152,553.20
Location: Mount Lavinia
Medals: 1
EC_Achievment (1)

Invitations sent: 44
Registered friends: 1
Reputation point: 37
Staff Sergeant

Re: How to get live currency rates using PHP

Postby asifqwq » Wed Jan 04, 2012 6:52 am

A really nice idea. I appreciate it much !
User avatar
asifqwq
 
Posts: 1
Joined: Wed Jan 04, 2012 6:23 am
Cash on hand: 125.80

Invitations sent: 0
Registered friends: 0
Reputation point: 0
Lance Corporal

Re: How to get live currency rates using PHP

Postby jaheen100 » Thu Jan 09, 2014 10:34 am

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
jaheen100
 
Posts: 10
Joined: Fri Jan 03, 2014 10:47 am
Cash on hand: 393.90

Invitations sent: 0
Registered friends: 0
Reputation point: 0
Lance Corporal

Re: How to get live currency rates using PHP

Postby Saman » Fri Jan 10, 2014 6:27 am

Where is the php code? Programmers here needs that, not a site  :)
User avatar
Saman
Support Team
Support Team
 
Posts: 812
Joined: Fri Jul 31, 2009 5:02 pm
Cash on hand: 152,553.20
Location: Mount Lavinia
Medals: 1
EC_Achievment (1)

Invitations sent: 44
Registered friends: 1
Reputation point: 37
Staff Sergeant


Return to PHP & MySQL

Who is online

Users browsing this forum: No registered users and 2 guests