diff --git a/cexapi.class.php b/cexapi.class.php index 5a77433..599c98b 100644 --- a/cexapi.class.php +++ b/cexapi.class.php @@ -6,7 +6,7 @@ * Jabber: t0pep0@jabber.ru * BTC : 1ipEA2fcVyjiUnBqUx7PVy5efktz2hucb * donate free =) - * + * * Update: 12/2/13 * Author: Zack Urben * Email : zackurben@gmail.com @@ -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 @@ -63,10 +63,10 @@ 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); @@ -74,44 +74,50 @@ private function post($url, $param = array()) { 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 @@ -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 @@ -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 @@ -139,7 +145,7 @@ 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 @@ -147,7 +153,7 @@ public function trade_history($since = 1, $couple = 'GHS/BTC') { 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 @@ -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 @@ -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. */