Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 57 additions & 33 deletions cexapi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Jabber: t0pep0@jabber.ru
* BTC : 1ipEA2fcVyjiUnBqUx7PVy5efktz2hucb
* donate free =)
*
*
* Update: 12/2/13
* Author: Zack Urben
* Email : zackurben@gmail.com
Expand Down Expand Up @@ -40,17 +40,17 @@ private function signature() {
$string = $this->nonce_v . $this->username . $this->api_key; //Create string
$hash = hash_hmac('sha256', $string, $this->api_secret); //Create hash
$hash = strtoupper($hash);

return $hash;
}

/**
* Set nonce as timestamp
*/
private function nonce() {
$this->nonce_v = round(microtime(true)*100);
}

/**
* Send post request to Cex.io API.
* @param string $url
Expand All @@ -63,55 +63,61 @@ private function post($url, $param = array()) {
foreach($param as $k => $v) {
$post .= $k . '=' . $v . '&'; //Dirty, but work
}

$post = substr($post, 0, strlen($post)-1);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'phpAPI');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$out = curl_exec($ch);

if (curl_errno($ch)) {
trigger_error("cURL failed. Error #".curl_errno($ch).": ".curl_error($ch), E_USER_ERROR);
}
curl_close($ch);

curl_close($ch);

return $out;
}
}

/**
* Send API call (over post request), to Cex.io server.
* @param string $method
* @param array $param
* @param string $private
* @param string $couple
* @param string $date
* @return array JSON results
*/
public function api_call($method, $param = array(), $private = false, $couple = '') {
$url = "https://cex.io/api/$method"; //Create url

public function api_call($method, $param = array(), $private = false, $couple = '', $date='') {
if($method == 'ohlcv/hd/'){
$url = "https://cex.io/api/$method".$date.'/'; //Create url
}
else{
$url = "https://cex.io/api/$method"; //Create url
}

if ($couple !== '') {
$url .= "$couple/"; //set couple if needed
$url .= "$couple"; //set couple if needed
}

if ($private === true) { //Create param
$param = array_merge(array(
'key' => $this->api_key,
'signature' => $this->signature(),
'nonce' => $this->nonce_v++), $param);
}

$answer = $this->post($url, $param);
$answer = json_decode($answer, true);

return $answer;
}

/**
* Get the current ticker results for the given pair, or 'GHS/BTC' by default.
* @param string $couple
Expand All @@ -120,7 +126,7 @@ public function api_call($method, $param = array(), $private = false, $couple =
public function ticker($couple = 'GHS/BTC') {
return $this->api_call('ticker/', array(), false, $couple);
}

/**
* Get the current bids and asks for the given pair, or 'GHS/BTC' by default.
* @param string $couple
Expand All @@ -129,7 +135,7 @@ public function ticker($couple = 'GHS/BTC') {
public function order_book($couple = 'GHS/BTC') {
return $this->api_call('order_book/', array(), false, $couple);
}

/**
* Get the current trade history for the given pair, or 'GHS/BTC' by default.
* @param int $since
Expand All @@ -139,15 +145,15 @@ public function order_book($couple = 'GHS/BTC') {
public function trade_history($since = 1, $couple = 'GHS/BTC') {
return $this->api_call('trade_history/', array("since" => $since), false, $couple);
}

/**
* Get the current account balance.
* @return array JSON results
*/
public function balance() {
return $this->api_call('balance/', array(), true);
}

/**
* Get the current account open orders for the given pair, or 'GHS/BTC' by default.
* @param string $couple
Expand All @@ -156,7 +162,16 @@ public function balance() {
public function open_orders($couple = 'GHS/BTC') {
return $this->api_call('open_orders/', array(), true, $couple);
}


/**
* Cancel all orders of a given pair for the account.
* @param string $couple
* @return boolean success
*/
public function cancel_all_order($couple = 'GHS/BTC') {
return $this->api_call('cancel_orders/', null, true, $couple);
}

/**
* Cancel the given order for the account.
* @param int $order_id
Expand All @@ -165,29 +180,38 @@ public function open_orders($couple = 'GHS/BTC') {
public function cancel_order($order_id) {
return $this->api_call('cancel_order/', array("id" => $order_id), true);
}

/**
* Place an order, with the given type, amount, price, and pair. Defaults to Buying 'GHS/BTC'.
* @param string $ptype
* @param float $amount
* @param float $price
* @param string $couple
* @param string $order_type
* @return array JSON order data
*/
public function place_order($ptype = 'buy', $amount = 1, $price = 1, $couple = 'GHS/BTC') {
return $this->api_call('place_order/', array(
"type" => $ptype,
"amount" => $amount,
"price" => $price), true, $couple);
public function place_order($ptype = 'buy', $amount = 1, $price = 1, $couple = 'GHS/BTC', $order_type = 'limit') {
if($order_type == 'market'){
return $this->api_call('place_order/', array(
"type" => $ptype,
"order_type" => $order_type,
"amount" => $amount), true, $couple);
}
elseif($price > 0){
return $this->api_call('place_order/', array(
"type" => $ptype,
"amount" => $amount,
"price" => $price), true, $couple);
}
}

/**
* Returns overall hash rate in MH/s.
*/
public function hashrate(){
return $this->api_call('ghash.io/hashrate', array(), true);
}

/**
* Returns workers' hash rate and rejected shares.
*/
Expand Down