From 59988fcfe02845205b47c916ab0c9e2dbfa74985 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 29 Oct 2018 06:38:36 +0000 Subject: [PATCH] bitmex --- README.md | 0 composer.json | 20 +++++ src/Api/ApiKey.php | 30 +++++++ src/Api/Order.php | 68 ++++++++++++++++ src/Api/User.php | 27 ++++++ src/Bitmex.php | 131 ++++++++++++++++++++++++++++++ src/Exceptions/Exception.php | 7 ++ src/Exceptions/OrderException.php | 7 ++ src/Exceptions/UserException.php | 7 ++ 9 files changed, 297 insertions(+) create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Api/ApiKey.php create mode 100644 src/Api/Order.php create mode 100644 src/Api/User.php create mode 100644 src/Bitmex.php create mode 100644 src/Exceptions/Exception.php create mode 100644 src/Exceptions/OrderException.php create mode 100644 src/Exceptions/UserException.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b3dc87a --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "bitmex", + "type": "library", + "description": "Bitmex API", + "license": "MIT", + "authors": [ + { + "name": "lin", + "email": "465382251@qq.com" + } + ], + "autoload": { + "psr-4": { + "Lin\\Bitmex\\": "./src/" + } + }, + "require": { + "guzzlehttp/guzzle": "^6.3" + } +} diff --git a/src/Api/ApiKey.php b/src/Api/ApiKey.php new file mode 100644 index 0000000..4dc5f50 --- /dev/null +++ b/src/Api/ApiKey.php @@ -0,0 +1,30 @@ +type='GET'; + $this->path='/api/v1/apiKey'; + + return $this->exec(); + } + + public function post(){ + + } + + public function postDisable(){ + + } + + public function delete($data){ + $this->type='DELETE'; + $this->path='/api/v1/apiKey'; + $this->data=$data; + + return $this->exec(); + } +} \ No newline at end of file diff --git a/src/Api/Order.php b/src/Api/Order.php new file mode 100644 index 0000000..d9ca34c --- /dev/null +++ b/src/Api/Order.php @@ -0,0 +1,68 @@ + + * */ + +namespace Lin\Bitmex\Api; + +use Lin\Bitmex\Bitmex; + +class Order extends Bitmex +{ + /** + * $data=[ + 'symbol'=>'ADAZ18', + + //可选参数 具体参考API https://testnet.bitmex.com/api/explorer/#!/Order/Order_getOrders + * 数量 + * 时间 + ]; + * */ + public function get($data){ + $this->type='GET'; + $this->path='/api/v1/order'; + $this->data=$data; + + return $this->exec(); + } + + public function put($data){ + + } + + /** + * $data=[ + 'symbol'=>'XBTUSD', + 'price'=>'10', + 'orderQty'=>'10', + 'ordType'=>'Limit', + ]; + * */ + public function post($data){ + $this->type='POST'; + $this->path='/api/v1/order'; + $this->data=$data; + + return $this->exec(); + } + + public function delete($data){ + + } + + public function putBulk($data){ + + } + + public function postBulk($data){ + + } + + public function postCancelAllAfter($data){ + + } + + public function postClosePosition($data){ + + } +} \ No newline at end of file diff --git a/src/Api/User.php b/src/Api/User.php new file mode 100644 index 0000000..24fd553 --- /dev/null +++ b/src/Api/User.php @@ -0,0 +1,27 @@ +type='GET'; + $this->path='/api/v1/user'; + + $this->exec(); + } + + public function put($data){ + $this->type='PUT'; + $this->path='/api/v1/user'; + $this->data=$data; + + $this->exec(); + } + + public function post($data){ + + } +} \ No newline at end of file diff --git a/src/Bitmex.php b/src/Bitmex.php new file mode 100644 index 0000000..8acd847 --- /dev/null +++ b/src/Bitmex.php @@ -0,0 +1,131 @@ + + * */ + +namespace Lin\Bitmex; + +use GuzzleHttp\Exception\RequestException; + +class Bitmex +{ + /** + * 是否开启bitmex测试账号,需要先去申请测试账号。 + * */ + private $test=false; + + protected $key=''; + + protected $secret=''; + + protected $host=''; + + protected $nonce=''; + + protected $signature=''; + + protected $headers=[]; + + protected $type=''; + + protected $path=''; + + protected $data=[]; + + protected $timeout=15; + + public function __construct(string $key,string $secret) + { + $this->key = $key; + + $this->secret=$secret; + + if(empty($host)) $this->host='https://www.bitmex.com'; + } + + /** + * 是否开启测试 + * */ + public function test($value=true){ + $this->test=$value; + + if($this->test) { + $this->host='https://testnet.bitmex.com'; + }else{ + $this->host='https://www.bitmex.com'; + } + } + + /** + * 认证 + * */ + protected function auth(){ + $this->nonce(); + + $this->signature(); + + $this->headers(); + } + + /** + * 过期时间 + * */ + protected function nonce(){ + $this->nonce = (string) number_format(round(microtime(true) * 100000), 0, '.', ''); + } + + /** + * 签名 + * */ + protected function signature(){ + $endata=http_build_query($this->data); + $this->signature=hash_hmac('sha256', $this->type.$this->path.$this->nonce.$endata, $this->secret); + } + + /** + * 默认头部信息 + * */ + protected function headers(){ + $this->headers=[ + 'accept' => 'application/json', + 'api-expires' => $this->nonce, + 'api-key'=>$this->key, + 'api-signature' => $this->signature, + ]; + + if(!empty($this->data)) $this->headers['content-type']='application/x-www-form-urlencoded'; + } + + /** + * 发送http + * */ + protected function send(){ + $client = new \GuzzleHttp\Client(); + + $data=[ + 'headers'=>$this->headers, + 'timeout'=>$this->timeout + ]; + + if(!empty($this->data)) $data['form_params']=$this->data; + + $response = $client->request($this->type, $this->host.$this->path, $data); + + return $response->getBody()->getContents(); + } + + /* + * 执行流程 + * */ + protected function exec(){ + $this->auth(); + + //可以记录日志 + try { + return $this->send(); + }catch (RequestException $e){ + //TODO 该流程记录日志 + print_r($e->getMessage()); + } + } +} \ No newline at end of file diff --git a/src/Exceptions/Exception.php b/src/Exceptions/Exception.php new file mode 100644 index 0000000..43b6d58 --- /dev/null +++ b/src/Exceptions/Exception.php @@ -0,0 +1,7 @@ +