Skip to content

Commit

Permalink
Add Request Options
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Aug 22, 2019
1 parent de25de5 commit 47f2bf1
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 96 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ Local development sets the proxy [More](https://github.com/zhouaini528/binance-p
```php
$binance=new Binance($key,$secret);

//If you are developing locally and need an agent, you can set this
$binance->setProxy();

//More flexible Settings
$binance->setProxy([
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
$binance->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,

//If you are developing locally and need an agent, you can set this
'proxy'=>true,
//More flexible Settings
'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
],

//Close the certificate
'verify'=>false,
]);
```

Expand Down
22 changes: 15 additions & 7 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ composer require "linwj/binance dev-master"
```php
$binance=new Binance($key,$secret);

//If you are developing locally and need an agent, you can set this
$binance->setProxy();

//More flexible Settings
$binance->setProxy([
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
$binance->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,

//If you are developing locally and need an agent, you can set this
'proxy'=>true,
//More flexible Settings
'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
],

//Close the certificate
'verify'=>false,
]);
```

Expand Down
41 changes: 9 additions & 32 deletions src/Binance.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class Binance
protected $secret;
protected $host;

protected $proxy=false;
protected $timeout=60;
protected $options=[];

function __construct(string $key='',string $secret='',string $host='https://api.binance.com'){
$this->key=$key;
Expand All @@ -33,58 +32,36 @@ private function init(){
'key'=>$this->key,
'secret'=>$this->secret,
'host'=>$this->host,
'timeout'=>$this->timeout,

'options'=>$this->options,
];
}

/**
* Local development sets the proxy
* @param bool|array
* $proxy=false Default
* $proxy=true Local proxy http://127.0.0.1:12333
*
* Manual proxy
* $proxy=[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
* ]
* */
function setProxy($proxy=true){
$this->proxy=$proxy;
}

/**
* Set the request timeout to 60 seconds by default
*
* */
function setTimeOut($timeout=60){
$this->timeout=$timeout;
function setOptions(array $options=[]){
$this->options=$options;
}

/**
*
* */
public function user(){
$user= new User($this->init());
$user->proxy($this->proxy);
return $user;
return new User($this->init());
}

/**
*
* */
public function system(){
$system= new System($this->init());
$system->proxy($this->proxy);
return $system;
return new System($this->init());
}

/**
*
* */
public function trade(){
$trade= new Trade($this->init());
$trade->proxy($this->proxy);
return $trade;
return new Trade($this->init());
}
}
51 changes: 22 additions & 29 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ class Request

protected $data=[];

protected $timeout=60;

protected $proxy=false;
protected $options=[];

public function __construct(array $data)
{
$this->key=$data['key'] ?? '';
$this->secret=$data['secret'] ?? '';
$this->host=$data['host'] ?? 'https://api.binance.com';

$this->timeout=$data['timeout'] ?? 60;
$this->options=$data['options'] ?? [];
}

/**
Expand All @@ -51,6 +49,8 @@ protected function auth(){
$this->signature();

$this->headers();

$this->options();
}

/**
Expand Down Expand Up @@ -85,39 +85,32 @@ protected function headers(){
}

/**
* 代理端口设置
* @param bool|array
* false 默认
* true 设置本地代理
* array 手动设置代理
* 请求设置
* */
function proxy($proxy=false){
$this->proxy=$proxy;
}

/**
* 发送http
* */
protected function send(){
$client = new \GuzzleHttp\Client();

$data=[
protected function options(){
$this->options=array_merge([
'headers'=>$this->headers,
'timeout'=>$this->timeout
];
//'verify'=>false //关闭证书认证
],$this->options);

//是否有代理设置
if(is_array($this->proxy)){
$data=array_merge($data,['proxy'=>$this->proxy]);
}else{
if($this->proxy) $data['proxy']=[
$this->options['timeout'] = $this->options['timeout'] ?? 60;

if($this->options['proxy']===true) {
$this->options['proxy']=[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
];
}

$response = $client->request($this->type, $this->host.$this->path.'?'.$this->signature, $data);
}

/**
* 发送http
* */
protected function send(){
$client = new \GuzzleHttp\Client();

$response = $client->request($this->type, $this->host.$this->path.'?'.$this->signature, $this->options);

$this->signature='';

Expand Down
21 changes: 14 additions & 7 deletions tests/proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@

$binance=new Binance($key,$secret);

//If you are developing locally and need an agent, you can set this
$binance->setProxy();

//More flexible Settings
$binance->setProxy([
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
$binance->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,

//If you are developing locally and need an agent, you can set this
'proxy'=>true,
//More flexible Settings
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//Close the certificate
'verify'=>false,
]);

//Get all account orders; active, canceled, or filled.
Expand Down
17 changes: 15 additions & 2 deletions tests/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@

$binance=new Binance();

//Set the request timeout to 60 seconds by default
//$binance->setTimeOut(10);
$binance->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,

//If you are developing locally and need an agent, you can set this
'proxy'=>true,
//More flexible Settings
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//Close the certificate
'verify'=>false,
]);

//Order book
try {
Expand Down
29 changes: 21 additions & 8 deletions tests/trade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,30 @@

$binance=new Binance($key,$secret);

//Set the request timeout to 60 seconds by default
//$binance->setTimeOut(10);
$binance->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,

//If you are developing locally and need an agent, you can set this
'proxy'=>true,
//More flexible Settings
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//Close the certificate
'verify'=>false,
]);

//Send in a new order.
try {
$result=$binance->trade()->postOrder([
'symbol'=>'BTCUSDT',
'side'=>'BUY',
'symbol'=>'BCHABCUSDT',
'side'=>'SELL',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'2000',
'quantity'=>'0.1',
'price'=>'999',
'timeInForce'=>'GTC',
]);
print_r($result);
Expand All @@ -38,7 +51,7 @@
//Check an order's status.
try {
$result=$binance->user()->getOrder([
'symbol'=>'BTCUSDT',
'symbol'=>'BCHABCUSDT',
'orderId'=>$result['orderId'],
'origClientOrderId'=>$result['clientOrderId'],
]);
Expand All @@ -50,7 +63,7 @@
//Cancel an active order.
try {
$result=$binance->trade()->deleteOrder([
'symbol'=>'BTCUSDT',
'symbol'=>'BCHABCUSDT',
'orderId'=>$result['orderId'],
'origClientOrderId'=>$result['clientOrderId'],
]);
Expand Down
21 changes: 17 additions & 4 deletions tests/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@

$binance=new Binance($key,$secret);

//Set the request timeout to 60 seconds by default
//$binance->setTimeOut(10);
$binance->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,

//If you are developing locally and need an agent, you can set this
'proxy'=>true,
//More flexible Settings
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//Close the certificate
'verify'=>false,
]);

//Get all account orders; active, canceled, or filled.
try {
$result=$binance->user()->getAllOrders([
'symbol'=>'BTCUSDT',
'symbol'=>'BCHABCUSDT',
'limit'=>'20',
//'orderId'=>'',
//'startTime'=>'',
Expand All @@ -33,7 +46,7 @@
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
die();

//Get current account information.
try {
$result=$binance->user()->getAccount();
Expand Down

0 comments on commit 47f2bf1

Please sign in to comment.