Skip to content

Instantly share code, notes, and snippets.

@mkraemer
Created November 15, 2013 12:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mkraemer/7483878 to your computer and use it in GitHub Desktop.
Save mkraemer/7483878 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 = [])
{
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 = [
'Content-Type: application/json-rpc',
'Authorization: Basic ' . $basicAuthentication,
'Json-Rpc-Tonce: ' . $tonce,
];
$postData = json_encode([
'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', [$price, $amount]);
return $response['result'];
}
public function sell($amount, $price)
{
$response = $this->request('sellOrder', [$price, $amount]);
return $response['result'];
}
}
@andyabc123
Copy link

Hi, how can i use this code, i got $key ,$secret thank you very much

@komdoisland
Copy link

Hi, I also want to know how to use this code. Much thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment