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 />";
?>