Source for file e-gold.class.php
Documentation is available at e-gold.class.php
* Class to manage e-Gold Account
* Author: Pawel Banasiak {@link http://banasiak.net}
* Thanks to wlk ({@link http://krzeslo.net})
* YOU USE THIS SCRIPT AT YOUR OWN RESPONSIBILITY AND AT YOUR OWN RISK
* @author Pawel Banasiak <banasiak@banasiak.net>
* @copyright Pawel Banasiak 2007
* @var int Connection timeout
public static $connection_timeout = 15;
* @var string Connection type - 'curl' or 'fsockopen' (fsockopen is not safety!)
public static $connection_type = 'curl';
private static $url_history = '/acct/historycsv.asp';
private static $url_balance = '/acct/balance.asp';
private static $url_verify = '/acct/verify.asp';
private static $url_spend = '/acct/confirm.asp';
private static $url_metaldata = '/unsecure/metaldata.asp';
private static $urlserver = 'www.e-gold.com';
private static $history_datas_default = array (
private static $EOL = "\r\n";
private static function connect($type, $params, $secure = true) {
$urlvar = 'url_' . $type;
foreach ($params as $key => $val) {
$params2 .= $key . '=' . $val . '&';
switch (self :: $connection_type) {
curl_setopt($ch, CURLOPT_URL, ($secure === true ? 'https://' : 'http://') . self :: $urlserver . self :: $ $urlvar);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, self :: $connection_timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$fp = fsockopen(($secure === true ? 'ssl://' : '') . self :: $urlserver, ($secure === true ? 443 : 80), $errno, $errstr, self :: $connection_timeout);
$out = 'POST ' . self :: $ $urlvar . ' HTTP/1.0' . self :: $EOL;
$out .= 'Host: ' . self :: $urlserver . self :: $EOL;
$out .= 'Content-Type: application/x-www-form-urlencoded' . self :: $EOL;
$out .= 'Content-Length: ' . strlen($params2) . self :: $EOL . self :: $EOL;
$out .= $params2 . self :: $EOL;
$out .= 'Connection: Close' . self :: $EOL . self :: $EOL;
while (!feof($fp) and $fp !== false) {
$response .= fgets($fp, 128);
return substr($response, strpos($response, self :: $EOL . self :: $EOL) + strlen(self :: $EOL . self :: $EOL));
* Shows history of account transactions
* @param int $account Number of e-Gold Account
* @param string $password Password to e-Gold Acount
* @param string $start Start date - must be in GNU format (see: {@link http://www.gnu.org/software/tar/manual/html_node/tar_113.html})
* @param string $end End date - must be in GNU format (see: {@link http://www.gnu.org/software/tar/manual/html_node/tar_113.html})
* @param array $datas see page 11 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf} default: paymentsmade = 1, paymentsreceived = 1, redemptions = 1, fees = 1
* @return int|string|csvReturns negative on script error, string on e-Gold error or csv data on success
public static function history($account, $password, $start = '-1 year', $end = 'now', $datas = null) {
$datas = self :: $history_datas_default;
$startdate = strtotime($start);
if ($startdate === false or $startdate == - 1)
if ($enddate === false or $enddate == - 1)
$params['AccountID'] = $account;
$params['PassPhrase'] = $password;
$params['startday'] = date('j', $startdate);
$params['startmonth'] = date('n', $startdate);
$params['startyear'] = date('Y', $startdate);
$params['endday'] = date('j', $enddate);
$params['endmonth'] = date('n', $enddate);
$params['endyear'] = date('Y', $enddate);
foreach ($datas as $key => $val) {
$return = self :: connect('history', $params);
* Shows current account balance
* @param int $account Number of e-Gold Account
* @param string $password Password to e-Gold Acount
* @param string $cur Currency of balance (see page 17 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}) default: USD
* @return int|arrayReturns negative on error or array on success
public static function balance($account, $password, $cur = 'USD') {
'PassPhrase' => $password
$return = self :: connect('balance', $params);
if (stristr($return, '<input type=hidden name=ERROR value=') !== false)
foreach ($vars as $val) {
if (preg_match('/<input type="?hidden"? name="?' . $val . '"? value="?([0-9\.]*)"?>/i', $return, $match) === false)
$out[$val] = (float) $match[1];
$return = self :: metaldata(null, null, $cur);
$out['Gold_Amount'] = $return[0][2] * $out['Gold_Ounces'];
$out['Silver_Amount'] = $return[0][3] * $out['Silver_Ounces'];
$out['Platinum_Amount'] = $return[0][4] * $out['Platinum_Ounces'];
$out['Palladium_Amount'] = $return[0][5] * $out['Palladium_Ounces'];
* Shows exchange rates of metals
* @param string $start Start date - must be in GNU format (see: {@link http://www.gnu.org/software/tar/manual/html_node/tar_113.html}) If not specified, you receive latest exchange rates
* @param string $end End date - must be in GNU format (see: {@link http://www.gnu.org/software/tar/manual/html_node/tar_113.html})
* @param string $cur Currency (see page 17 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}) default: USD
* @return int|arrayReturns negative on error or array on success
public static function metaldata($start = null, $end = null, $cur = 'USD') {
if ($start !== null and $end !== null) {
if ($startdate === false or $startdate == - 1)
if ($enddate === false or $enddate == - 1)
'StartDate' => $startdate,
$return = self :: connect('metaldata', $params, false);
if (preg_match_all('#([0-9]+/[0-9]+/[0-9]{4}\s*[0-9]+:[0-9]+:[0-9]+\s*(?:PM|AM)),\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*#i', $return, $matches, PREG_SET_ORDER) === false)
* Verifies if transaction is possible
* @param int $account Number of e-Gold Account
* @param string $password Password to e-Gold Acount
* @param int $payee Payee Account Number
* @param float $amount Amount of transaction
* @param string $worthof see page 9 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @param int $payin see page 9 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @param string $memo see page 9 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @param string $paymentid see page 9 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @return int|string|arrayReturn negative on script error, string on e-Gold error or array on success
public static function verify($account, $password, $payee, $amount, $worthof = "Gold", $payin = 1, $memo = null, $paymentid = null) {
'PassPhrase' => $password,
'Payee_Account' => $payee,
'PAYMENT_ID' => $paymentid
$return = self :: connect('verify', $params);
if (preg_match('/<input type="?hidden"? name="?ERROR"? value="?([^">]*)"?>/i', $return, $match) === false)
foreach ($vars as $val) {
if (preg_match('/<input type="?hidden"? name="?' . $val . '"? value="?([^">]*)"?>/i', $return, $match) === false)
* @param int $account Number of e-Gold Account
* @param string $password Password to e-Gold Acount
* @param int $payee Payee Account Number
* @param float $amount Amount of transaction
* @param string $memo see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @param int $payin see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @param string $worthof see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @param string $ounces see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @param string $ignoreratechange see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @param string $paymentid see page 7 from {@link http://www.e-gold.com/docs/e-gold_automation.pdf}
* @return int|string|arrayReturns negative on script error, string on e-Gold error or array on success
public static function spend($account, $password, $payee, $amount, $memo, $payin = 1, $worthof = 'Gold', $ounces = null, $ignoreratechange = 'y', $paymentid = null) {
'PassPhrase' => $password,
'Payee_Account' => $payee,
'ACTUAL_PAYMENT_OUNCES' => $ounces,
'IGNORE_RATE_CHANGE' => $ignoreratechange,
'PAYMENT_ID' => $paymentid
$return = self :: connect('spend', $params);
if (preg_match('/<input type="?hidden"? name="?ERROR"? value="?([^">]*)"?>/i', $return, $match) === false)
foreach ($vars as $val) {
if (preg_match('/<input type="?hidden"? name="?' . $val . '"? value="?([^">]*)"?>/i', $return, $match) === false)
* For mor information see {@link http://www.e-gold.com/docs/e-gold_sci.pdf}
* @param string $alternatepassword Alternate password to e-Gold Account
* @param bool $apinmd5 Set TRUE if in $alternatepassword you specified md5 hash of Alternate password
* @param array $datas Specify only if payment informations is not in $_POST
* @param string $ip Specify only if IP address are not in $_SERVER['REMOTE_ADDR']
* @return int Return negative on error (-1 on bad ip address -2 on bad hash) or 1 on success
public static function sci($alternatepassword = null, $apinmd5 = false, $datas = null, $ip = null) {
if (substr($_SERVER['REMOTE_ADDR'], 0, 11) != '63.240.230.')
if (substr($ip, 0, 11) != '63.240.230.')
if ($alternatepassword != null) {
$hash = strtoupper(md5($v['PAYMENT_ID'] . ':' . $v['PAYEE_ACCOUNT'] . ':' . $v['PAYMENT_AMOUNT'] . ':' . $v['PAYMENT_UNITS'] . ':' . $v['PAYMENT_METAL_ID'] . ':' . $v['PAYMENT_BATCH_NUM'] . ':' . $v['PAYER_ACCOUNT'] . ':' . strtoupper(($apinmd5 === false ? md5($alternatepassword) : $alternatepassword)) . ':' . $v['ACTUAL_PAYMENT_OUNCES'] . ':' . $v['USD_PER_OUNCE'] . ':' . $v['FEEWEIGHT'] . ':' . $v['TIMESTAMPGMT']));
if ($v['V2_HASH'] != $hash)
|