Skip to content

Instantly share code, notes, and snippets.

@Cazz0r
Forked from mkraemer/BTCChina API
Last active December 29, 2015 01:19
Show Gist options
  • Save Cazz0r/7592813 to your computer and use it in GitHub Desktop.
Save Cazz0r/7592813 to your computer and use it in GitHub Desktop.
<?php
namespace Bitcoin\BTCChina;
class API
{
protected $key;
protected $secret;
public function __construct($key, $secret)
{
$this->key = $key;
$this->secret = $secret;
}
public function request($method, $methodParameters = array())
{
list($usec, $sec) = explode(' ', microtime());
$tonce = $sec . substr($usec, 2, 6);
$queryString = sprintf(
'tonce=%s&accesskey=%s&requestmethod=post&id=%s&method=%s&params=%s',
$tonce,
$this->key,
$tonce,
$method,
implode(',', $methodParameters)
);
$hash = hash_hmac('sha1',$queryString, $this->secret);
$basicAuthentication = base64_encode($this->key . ':' . $hash);
$headers = array(
'Content-Type: application/json-rpc',
'Authorization: Basic ' . $basicAuthentication,
'Json-Rpc-Tonce: ' . $tonce,
);
$postData = json_encode(array(
'method' => $method,
'params' => $methodParameters,
'id' => $tonce
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.btcchina.com/api_trade_v1.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($ch);
return json_decode($res, true);
}
public function cpm()
{
return 0;
}
public function openOrders()
{
$orders = $this->request('getOrders');
return $orders['result']['order'];
}
public function getAccountInfo()
{
$accountInfo = $this->request('getAccountInfo');
return $accountInfo['result'];
}
public function marketDepth()
{
$response = $this->request('getMarketDepth2');
return $response['result']['market_depth'];
}
public function buy($amount, $price)
{
$response = $this->request('buyOrder', array($price, $amount));
return $response['result'];
}
public function sell($amount, $price)
{
$response = $this->request('sellOrder', array($price, $amount));
return $response['result'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment