Created
September 20, 2017 12:05
-
-
Save plann9/1fe6fc1cc4bcf83699357066ced3c4e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Hitbtc; | |
class TradingApi { | |
const HITBTC_API_URL = 'http://api.hitbtc.com'; | |
const HITBTC_TRADING_API_BASE_URI = '/api/1/trading/'; | |
private $key; | |
private $secret; | |
public function __construct($key, $secret) { | |
$this->key = $key; | |
$this->secret = $secret; | |
} | |
public function getBalance() { | |
return $this->request('balance'); | |
} | |
public function getActiveOrders($arguments) { | |
return $this->request('orders/active', $arguments); | |
} | |
public function getRecentOrders($arguments) { | |
return $this->request('orders/recent', $arguments); | |
} | |
public function getTrades($arguments) { | |
return $this->request('trades', $arguments); | |
} | |
public function newOrder($arguments) { | |
return $this->request('new_order', $arguments); | |
} | |
public function cancelOrder($arguments) { | |
return $this->request('cancel_order', $arguments); | |
} | |
protected function request($method, $arguments = array()) { | |
$uri = self::HITBTC_TRADING_API_BASE_URI . $method . '?nonce=' . $this->getNonce() . '&apikey=' . $this->key; | |
$url = self::HITBTC_API_URL . $uri; | |
$postData = $arguments ? http_build_query($arguments, '', '&') : null; | |
$signature = $this->getRequestSignature($uri, $postData ? $postData : null); | |
$curlOptions = array( | |
CURLOPT_CONNECTTIMEOUT => 10, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_URL => $url, | |
CURLOPT_HTTPHEADER => array('X-Signature: ' . $signature) | |
); | |
if($postData) { | |
$curlOptions[CURLOPT_POST] = true; | |
$curlOptions[CURLOPT_POSTFIELDS] = $postData; | |
} | |
$ch = curl_init(); | |
curl_setopt_array($ch, $curlOptions); | |
$result = curl_exec($ch); | |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
$error = curl_error($ch); | |
curl_close($ch); | |
if($error || ($code != 200 && $code != 204)) { | |
throw new TradingApiRequestFailed("Request to '$url' with POST data $postData failed with code $code and error: " . ($error ? $error : $result), $code); | |
} | |
if($result && ($resultArray = @json_decode($result, true)) !== false) { | |
return $resultArray; | |
} | |
return $result; | |
} | |
private function getRequestSignature($uri, $postData = null) { | |
return strtolower(hash_hmac('sha512', $uri . $postData, $this->secret)); | |
} | |
private function getNonce() { | |
return round(microtime(true) * 10000); | |
} | |
} | |
class TradingApiRequestFailed extends \Exception { | |
} | |
// Example | |
$api = new TradingApi('key', 'secret'); | |
var_dump($api->getBalance()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment