diff --git a/README.md b/README.md index 6768ad5a..ff8eb72c 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,21 @@ require 'vendor/autoload.php'; $api = new Binance\API("",""); ``` +#### Getting started with proxy +```php +require 'vendor/autoload.php'; + +$proxyConf = [ + 'proto' => 'tcp', + 'address' => '192.168.1.1', + 'port' => '8080', + 'user' => 'dude', + 'pass' => 'd00d' +]; + +$api = new Binance\API("","", ['useServerTime'=>false], $proxyConf); +``` + #### Get latest price of a symbol ```php $ticker = $api->prices(); diff --git a/examples/proxy.php b/examples/proxy.php new file mode 100644 index 00000000..9761441e --- /dev/null +++ b/examples/proxy.php @@ -0,0 +1,29 @@ + 'tcp', + 'address' => '192.168.1.1', + 'port' => '8080', + 'user' => 'dude', + 'pass' => 'd00d' +]; + +$api = new Binance\API("","",["useServerTime"=>false],$proxyConf); + +$ticker = $api->prices(); +print_r($ticker); // List prices of all symbols +echo "Price of BNB: {$ticker['BNBBTC']} BTC.".PHP_EOL; + +// Get balances for all of your positions, including estimated BTC value +$balances = $api->balances($ticker); +print_r($balances); +echo "BTC owned: ".$balances['BTC']['available'].PHP_EOL; +echo "ETH owned: ".$balances['ETH']['available'].PHP_EOL; +echo "Estimated Value: ".$api->btc_value." BTC".PHP_EOL; + +// Getting 24hr ticker price change statistics for a symbol +$prevDay = $api->prevDay("BNBBTC"); +print_r($prevDay); +echo "BNB price change since yesterday: ".$prevDay['priceChangePercent']."%".PHP_EOL; diff --git a/php-binance-api.php b/php-binance-api.php index 10af0543..065355b8 100644 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -15,12 +15,15 @@ class API { protected $chartQueue = []; protected $charts = []; protected $info = ["timeOffset"=>0]; + protected $proxyConf = null; public $balances = []; public $btc_value = 0.00; // value of available assets public $btc_total = 0.00; // value of available + onOrder assets - public function __construct($api_key = '', $api_secret = '', $options = ["useServerTime"=>false]) { + public function __construct($api_key = '', $api_secret = '', $options = ["useServerTime"=>false], $proxyConf = null ) { $this->api_key = $api_key; $this->api_secret = $api_secret; + $this->proxyConf = $proxyConf; + if ( isset($options['useServerTime']) && $options['useServerTime'] ) { $this->useServerTime(); } @@ -106,6 +109,30 @@ public function balances($priceData = false) { return $this->balanceData($this->signedRequest("v3/account"),$priceData); } + private function getProxyUriString() + { + $uri = ""; + $uri .= isset( $this->proxyConf['proto'] ) ? $this->proxyConf['proto'] : "tcp"; + $uri .= "://"; + $uri .= isset( $this->proxyConf['address'] ) ? $this->proxyConf['address'] : "localhost"; + if( isset( $this->proxyConf['address'] ) == false ) echo "warning: proxy address not set defaulting to localhost\n"; + $uri .= ":"; + $uri .= isset( $this->proxyConf['port'] ) ? $this->proxyConf['port'] : "1080"; + if( isset( $this->proxyConf['address'] ) == false ) echo "warning: proxy port not set defaulting to 1080\n"; + + return $uri; + } + + private function appendProxyAuthString() + { + if( isset($this->proxyConf['user']) && isset($this->proxyConf['pass']) ) + { + $auth = base64_encode( $this->proxyConf['user'] . ':' . $this->proxyConf['pass']); + return "Proxy-Authorization: Basic $auth\r\n"; + } + return ""; + } + private function request($url, $params = [], $method = "GET") { $opt = [ "http" => [ @@ -114,6 +141,13 @@ private function request($url, $params = [], $method = "GET") { "header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\n" ] ]; + + if( is_array($this->proxyConf) ) { + $opt['http']['request_fulluri'] = true; + $opt['http']['proxy'] = $this->getProxyUriString(); + $opt['http']['header'] .= $this->appendProxyAuthString(); + } + $context = stream_context_create($opt); $query = http_build_query($params, '', '&'); try { @@ -135,6 +169,13 @@ private function signedRequest($url, $params = [], $method = "GET") { "header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\nX-MBX-APIKEY: {$this->api_key}\r\n" ] ]; + + if( is_array($this->proxyConf) ) { + $opt['http']['request_fulluri'] = true; + $opt['http']['proxy'] = $this->getProxyUriString(); + $opt['http']['header'] .= $this->appendProxyAuthString(); + } + $context = stream_context_create($opt); $ts = (microtime(true)*1000) + $this->info['timeOffset']; $params['timestamp'] = number_format($ts,0,'.',''); @@ -166,6 +207,13 @@ private function apiRequest($url, $method = "GET") { "header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\nX-MBX-APIKEY: {$this->api_key}\r\n" ] ]; + + if( is_array($this->proxyConf) ) { + $opt['http']['request_fulluri'] = true; + $opt['http']['proxy'] = $this->getProxyUriString(); + $opt['http']['header'] .= $this->appendProxyAuthString(); + } + $context = stream_context_create($opt); try { $data = file_get_contents($this->base.$url, false, $context); @@ -203,7 +251,7 @@ public function candlesticks($symbol, $interval = "5m", $limit = null, $startTim if ($limit) $opt["limit"] = $limit; if ($startTime) $opt["startTime"] = $startTime; if ($endTime) $opt["endTime"] = $endTime; - + $response = $this->request("v1/klines", $opt); $ticks = $this->chartData($symbol, $interval, $response); $this->charts[$symbol][$interval] = $ticks; @@ -267,7 +315,7 @@ private function balanceHandler($json) { } return $balances; } - + // Convert WebSocket ticker data into array private function tickerStreamHandler($json) { return [