Realtime Currency Conversion extracting from XE.com

Follow by Email
Facebook
Google+
http://xtremepassion.leladystore.com/2011/04/realtime-currency-conversion-extracting-from-xe-com/
YouTube
PINTEREST
INSTAGRAM

This post is to show you how to extracts the conversion rate from xe.com (The world favorite Currency Site) using PHP. Sounds like Hacking huh!? Reminder from xe.com: “WARNING: Automated extraction of rates is prohibited under the Terms of Use”. I will show you the way how to do it, but try it at your own risk (not recommended if your system or application is for commercial usage).

<?php
//save as class_xe_curr_conv.php
//defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
/**
*	CURRENCYCONVERTER
*	@Original Date Feb 23,2005 
*	@Original Author Harish Chauhan Email - harishc@ultraglobal.biz
*	@modified Date August 3, 2006 
*	@modified Author CiPHeR - jeff@wickedwebwerkz.com
*
*	ABOUT
*	This PHP script will use for conversion of currency. you can find it is tricky but it is usefull.
*	Modified by CiPHeR as an alternative to VM's default currency_convert.php class file
*	Some instances did not like to pass $price['product_price'] to convertECB( $price['product_price'], 'CAD', 'USD' )
*	This version seems to work well
**/

class CURRENCYCONVERTER {
	var $_amt=1;
	var $_to="";
	var $_from="";
	var $_error=""; 

	function CURRENCYCONVERTER($amt="",$to="",$from="")	{
		$this->_amt=$amt;
		$this->_to=$to;
		$this->_from=$from;
	}

	function error() {
		return $this->_error;
	}

	function convert($amt=NULL,$to="",$from='USD') {
		if($amt>0)
			$this->_amt=$amt;
		if(!empty($to))
			$this->_to=$to;
		if(!empty($from))
			$this->_from=$from;

		$host="www.xe.com";
		$fp = @fsockopen($host, 80, $errno, $errstr, 240);
		if (!$fp) {
			$this->_error="$errstr ($errno)<br />\n";
			return false;
		} else {
			$file1="/ucc/convert.cgi";
			$str1 = "?language=xe&Amount=".$this->_amt."&From=".$this->_from."&To=".$this->_to;
			
			$out = "GET ".$file1.$str1." HTTP/1.0\r\n";
		    $out .= "Host: $host\r\n";
			$out .= "Connection: Close\r\n\r\n";

			@fputs($fp, $out);
			while (!@feof($fp)) {
				$data.= @fgets($fp,128);
			}
			@fclose($fp);
			
			@preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $data, $match);
			
			$data =$match[2];
			$search = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
				"'<[\/\!]*?[^<>]*?>'si",           // Strip out HTML tags
				"'([\r\n])[\s]+'",                 // Strip out white space
				"'&(quot|#34);'i",                 // Replace HTML entities
				"'&(amp|#38);'i",
				"'&(lt|#60);'i",
				"'&(gt|#62);'i",
				"'&(nbsp|#160);'i",
				"'&(iexcl|#161);'i",
				"'&(cent|#162);'i",
				"'&(pound|#163);'i",
				"'&(copy|#169);'i",
				"'&#(\d+);'e");                    // evaluate as php

			$replace = array ("",
				"",
				"\\1",
				"\"",
				"&",
				"<",
				">",
				" ",
				chr(161),
				chr(162),
				chr(163),
				chr(169),
				"chr(\\1)");

			$data = @preg_replace($search, $replace, $data);			
			@preg_match_all("/(\d[^\.\:]*(\.\d+)?)\s$this->_to/",$data,$mathces);			
			$return=preg_replace("/[^\d\.]*/","",$mathces[0][1]);
			
			return (double)substr($return,2);
		}
	}
}
?>

Figure1: class_xe_curr_conv.php

<?php
//save as mymoneyconv.php
//display currency conversion
require_once("class_xe_curr_conv.php");

/* variables or parameters
 * $in_amt - amount to be converted
 * $in_cto - convert to currency
 * $in_basecur - base currency
**/

function displayMyMoney($in_amt, $in_cto, $in_basecur){
	$m = new CURRENCYCONVERTER(); //instantiate an object
	return $m->convert( $in_amt, $in_cto, $in_basecur );	
}

//change to any values you would like to convert to
$tousd = 0.3;
$tojpy = 10;

echo 'RM' . $tousd . ' in USD: ' . displayMyMoney($tousd, 'USD', 'MYR') . "<br/>";
echo 'RM' . $tojpy . ' in JPY: ' . displayMyMoney($tojpy, 'JPY', 'MYR') . "<br/>";

?>

Figure2: mymoneyconv.php

Output:
RM0.3 in USD: 0.0992064
RM10 in JPY: 275.636

*** HAPPY CODING & REMEMBER TO SUPPORT MY BLOG ***

Leave a Reply

Your email address will not be published. Required fields are marked *

1,796 views