Jump to content

Alternative currency rate fetch & update script


Recommended Posts

This is an alternative script to update currency rates.

 

WHMCS only updates certain currency rates.

You may find this script useful if:

- You need to update currencies not updated by WHMCS

- You wish to "massage" the rates (for example, if you use 2Checkou you may want to increase the rates by 4-5% for a closer match to 2Checkout's rates)

 

The script fetches the currency rates from Google, stores the rates in the WHMCS db, and updates the product prices. You can run the script by cronjob once a day.

It's a good idea to place the script in a directory not accessible by the web.

You must have a file named cookie.txt (with permissions 777) in the same directory where you run the script from.

 


<?php

function exchangeRate( $amount, $currency, $exchangeIn )
{
   $googleQuery = $amount . ' ' . $currency . ' in ' . $exchangeIn;
   $googleQuery = urlEncode( $googleQuery );
   $askGoogle = file_get_contents( 'http://www.google.com/search?q=' . $googleQuery );
   $askGoogle = strip_tags( $askGoogle );
   $matches = array();
   preg_match( '/= (([0-9]|\.|,|\ )*)/', $askGoogle, $matches );
   return $matches[1] ? $matches[1] : false;
}


function open_https_url($url,$refer = "",$usecookie = false) {
if ($usecookie) {
	if (file_exists($usecookie)) {
		if (!is_writable($usecookie)) {
			return "Can't write to $usecookie cookie file, change file permission to 777 or remove read only for windows.";
		}
	} else {
		$usecookie = "cookie.txt";
		if (!is_writable($usecookie)) {
			return "Can't write to $usecookie cookie file, change file permission to 777 or remove read only for windows.";
		}
	}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)");
if ($usecookie) {
	curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie);
	curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);
}
if ($refer != "") {
	curl_setopt($ch, CURLOPT_REFERER, $refer );
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result =curl_exec ($ch);
curl_close ($ch);
return $result;
}


echo "fetching EUR rate... <br />";
$reur = exchangeRate( 1, 'usd','eur');
echo " 1 USD = " .$reur. "EUR <br /><br />";
echo " Updating database... <br />";
mysql_connect("localhost", "db_username", "db_password") or die(mysql_error());
mysql_select_db("whmcs_db") or die(mysql_error());
mysql_query("UPDATE tblcurrencies SET rate = '$reur' WHERE code = 'EUR'");
echo "done updating db  <br />";
echo "--------------------------------------------- <br /><br />";

echo "fetching GBP rate... <br />";
$rgbp = exchangeRate( 1, 'usd','gbp');
echo " 1 USD = " .$rgbp. "GBP <br /><br />";
echo " Updating database... <br />";
mysql_connect("localhost", "db_username", "db_password") or die(mysql_error());
mysql_select_db("whmcs_db") or die(mysql_error());
mysql_query("UPDATE tblcurrencies SET rate = '$rgbp' WHERE code = 'gbp'");
echo "done updating db  <br />";
echo "--------------------------------------------- <br /><br />";

echo "fetching CAD rate... <br />";
$rcad = exchangeRate( 1, 'usd','cad');
echo " 1 USD = " .$rcad. "CAD <br /><br />";
echo " Updating database... <br />";
mysql_connect("localhost", "db_username", "db_password") or die(mysql_error());
mysql_select_db("whmcs_db") or die(mysql_error());
mysql_query("UPDATE tblcurrencies SET rate = '$rcad' WHERE code = 'cad'");
echo "done updating db  <br />";
echo "--------------------------------------------- <br /><br />";

echo "fetching AUD rate... <br />";
$raud = exchangeRate( 1, 'usd','aud');
echo " 1 USD = " .$raud. "AUD <br /><br />";
echo " Updating database... <br />";
mysql_connect("localhost", "db_username", "db_password") or die(mysql_error());
mysql_select_db("whmcs_db") or die(mysql_error());
mysql_query("UPDATE tblcurrencies SET rate = '$raud' WHERE code = 'aud'");
echo "done updating db  <br />";
echo "--------------------------------------------- <br /><br />";

echo "fetching MXN rate... <br />";
$rmxn = exchangeRate( 1, 'usd','mxn');
echo " 1 USD = " .$rmxn. "MXN <br /><br />";
echo " Updating database... <br />";
mysql_connect("localhost", "db_username", "db_password") or die(mysql_error());
mysql_select_db("whmcs_db") or die(mysql_error());
mysql_query("UPDATE tblcurrencies SET rate = '$rmxn' WHERE code = 'mxn'");
echo "done updating db  <br />";
echo "--------------------------------------------- <br /><br />";

echo "fetching ARS rate... <br />";
$rars = exchangeRate( 1, 'usd','ars');
echo " 1 USD = " .$rars. "ARS <br /><br />";
echo " Updating database... <br />";
mysql_connect("localhost", "db_username", "db_password") or die(mysql_error());
mysql_select_db("whmcs_db") or die(mysql_error());
mysql_query("UPDATE tblcurrencies SET rate = '$rars' WHERE code = 'ars'");
echo "done updating db  <br />";
echo "--------------------------------------------- <br /><br />";

echo "fetching CLP rate... <br />";
$rclp = exchangeRate( 1, 'usd','clp');
echo " 1 USD = " .$rclp. "CLP <br /><br />";
echo " Updating database... <br />";
mysql_connect("localhost", "db_username", "db_password") or die(mysql_error());
mysql_select_db("whmcs_db") or die(mysql_error());
mysql_query("UPDATE tblcurrencies SET rate = '$rclp' WHERE code = 'clp'");
echo "done updating db  <br />";
echo "--------------------------------------------- <br /><br />";

echo "fetching PEN rate... <br />";
$rpen = exchangeRate( 1, 'usd','pen');
echo " 1 USD = " .$rpen. "PEN <br /><br />";
echo " Updating database... <br />";
mysql_connect("localhost", "db_username", "db_password") or die(mysql_error());
mysql_select_db("whmcs_db") or die(mysql_error());
mysql_query("UPDATE tblcurrencies SET rate = '$rpen' WHERE code = 'pen'");
echo "done updating db  <br />";
echo "--------------------------------------------- <br /><br />";

echo "Loging in to WHMCS...<br /><br />";
open_https_url("https://yourdomain.com/whmcsdir/admin/dologin.php?username=whmcsusername&password=whmcspassword","",true);
echo "Updating product prices...<br /><br />";
open_https_url("https://yourdomain.com/whmcsdir/admin/configcurrencies.php?updateprices=true","",true);
echo "done updating product prices  <br />";
echo "--------------------------------------------- <br /><br />";

?>

Link to comment
Share on other sites

  • 4 months later...

awesome- this is exactly what we are looking for!

 

We need this for shekels. Would you be able to write the script to pull rates from one of these sites?:

http://bankisrael.gov.il/heb.shearim/index.php

http://www.leumi.co.il/home04/currency_rates/9034/

http://www.xe.com/ucc/convert.cgi?Amount=1&From=USD&To=ILS&image.x=28&image.y=12&image=Submit

 

Thank you!

Link to comment
Share on other sites

script is not working, I'm getting the following errors:

 

/home/hostingpath/CUSTOM_SCRIPTS/rate_update.php: line 1: ?php

: No such file or directory

/home/hostingpath/CUSTOM_SCRIPTS/rate_update.php: line 2:

: command not found

/home/hostingpath/CUSTOM_SCRIPTS/rate_update.php: line 3: syntax error near unexpected token `$amount,'

/home/hostingpath/CUSTOM_SCRIPTS/rate_update.php: line 3: `function exchangeRate( $amount, $currency, $exchangeIn )

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated