cex.io rest-api (public + private)

no trading or markets yet, just made to query balances
This commit is contained in:
Tanguy Pruvot 2018-01-02 10:22:20 +01:00
parent 80fa212c23
commit dfb28223cc
6 changed files with 119 additions and 0 deletions

View file

@ -10,6 +10,7 @@ define('EXCH_BITSTAMP_SECRET','');
define('EXCH_BLEUTRADE_SECRET', '');
define('EXCH_BTER_SECRET', '');
define('EXCH_CCEX_SECRET', '');
define('EXCH_CEXIO_SECRET', '');
define('EXCH_COINMARKETS_PASS', '');
define('EXCH_CRYPTOPIA_SECRET', '');
define('EXCH_EMPOEX_SECKEY', '');

View file

@ -127,6 +127,11 @@ class ExchangeCommand extends CConsoleCommand
if (!is_array($balance)) echo "bitstamp error ".json_encode($balance)."\n";
else echo("bitstamp: ".json_encode($balance)."\n");
}
if (!empty(EXCH_CEXIO_KEY)) {
$balance = cexio_api_user('balance');
if (!is_array($balance)) echo "cexio error ".json_encode($balance)."\n";
else echo("cexio: ".json_encode(arraySafeVal($balance,"BTC",$balance))."\n");
}
if (!empty(EXCH_BITTREX_KEY)) {
$balance = bittrex_api_query('account/getbalance','&currency=BTC');
if (!is_object($balance)) echo "bittrex error\n";

View file

@ -0,0 +1,107 @@
<?php
// cex.io api queries - tpruvot 2018
// see https://cex.io/rest-api for the methods
function cexio_api_query($method, $params='')
{
$url = "https://cex.io/api/$method/";
if (!empty($params)) $url .= "$params";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP API');
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$execResult = curl_exec($ch);
$res = json_decode($execResult, true);
return $res;
}
function cexio_api_user($method, $params=array())
{
require_once('/etc/yiimp/keys.php');
if (!defined('EXCH_CEXIO_KEY')) return false;
if (!defined('EXCH_CEXIO_SECRET')) return false;
if (!defined('EXCH_CEXIO_ID')) return false;
$username = EXCH_CEXIO_ID;
$apikey = EXCH_CEXIO_KEY; // your API-key
$apisecret = EXCH_CEXIO_SECRET; // your Secret-key
if (empty($username) || empty($apikey) || empty($apisecret)) return false;
$mt = explode(' ', microtime());
$nonce = $mt[1].substr($mt[0], 2, 6);
$nonce = $mt[1];
$msg = "{$nonce}{$username}{$apikey}";
$sha = hash_hmac('sha256', $msg, $apisecret);
$sign = strtoupper($sha);
$url = "https://cex.io/api/$method/";
$postdata = array(
'key' => $apikey,
'signature' => $sign,
'nonce' => $nonce
);
if (!empty($params)) {
foreach($params as $k=>$v) $postdata[$k] = $v;
}
$post_data = http_build_query($postdata, '', '&');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//curl_setopt($ch, CURLOPT_SSLVERSION, 1 /*CURL_SSLVERSION_TLSv1*/);
curl_setopt($ch, CURLOPT_SSL_SESSIONID_CACHE, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; cex.io API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
curl_setopt($ch, CURLOPT_ENCODING , '');
$execResult = curl_exec($ch);
$res = json_decode($execResult, true);
return $res;
}
// https://cex.io/rest-api#ticker
function cexio_btceur()
{
$ticker = cexio_api_query('ticker', 'BTC/EUR');
return is_array($ticker) ? floatval($ticker["last"]) : false;
}
function cexio_btcusd()
{
$ticker = cexio_api_query('ticker', 'BTC/USD');
return is_array($ticker) ? floatval($ticker["last"]) : false;
}
// https://cex.io/rest-api#account-balance
function getCexIoBalances()
{
$exchange = 'cexio';
if (exchange_get($exchange, 'disabled')) return;
$savebalance = getdbosql('db_balances', "name='$exchange'");
if (is_object($savebalance)) {
$balances = cexio_api_user('balance');
if (is_array($balances)) {
$b = arraySafeVal($balances, 'BTC');
$savebalance->balance = arraySafeVal($b, 'available');
$savebalance->save();
}
}
}

View file

@ -18,6 +18,7 @@ require_once("bitstamp.php");
require_once("bittrex.php");
require_once("ccexapi.php");
require_once("bleutrade.php");
require_once("cexio.php");
require_once("kraken.php");
require_once("yobit.php");
require_once("shapeshift.php");
@ -82,6 +83,8 @@ function getMarketUrl($coin, $marketName)
$url = "https://bleutrade.com/exchange/{$symbol}/{$base}";
else if($market == 'bter')
$url = "https://bter.com/trade/{$lowsymbol}_{$lowbase}";
else if($market == 'cexio')
$url = "https://cex.io/trade/{$symbol}-{$base}";
else if($market == 'coinexchange')
$url = "https://www.coinexchange.io/market/{$symbol}/{$base}";
else if($market == 'coinsmarkets')

View file

@ -32,6 +32,8 @@ if (!defined('EXCH_BITSTAMP_KEY')) define('EXCH_BITSTAMP_KEY','');
if (!defined('EXCH_BLEUTRADE_KEY')) define('EXCH_BLEUTRADE_KEY', '');
if (!defined('EXCH_BTER_KEY')) define('EXCH_BTER_KEY', '');
if (!defined('EXCH_CCEX_KEY')) define('EXCH_CCEX_KEY', '');
if (!defined('EXCH_CEXIO_ID')) define('EXCH_CEXIO_ID', '');
if (!defined('EXCH_CEXIO_KEY')) define('EXCH_CEXIO_KEY', '');
if (!defined('EXCH_CRYPTOPIA_KEY')) define('EXCH_CRYPTOPIA_KEY', '');
if (!defined('EXCH_HITBTC_KEY')) define('EXCH_HITBTC_KEY', '');
if (!defined('EXCH_POLONIEX_KEY')) define('EXCH_POLONIEX_KEY', '');

View file

@ -132,6 +132,7 @@ class CronjobController extends CommonController
if(!YAAMP_PRODUCTION) break;
getBitstampBalances();
getCexIoBalances();
doBittrexTrading();
doCryptopiaTrading();
doKrakenTrading();