Skip to content

Commit

Permalink
Add proxy support (dmzoneill)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Eyrick authored Mar 8, 2018
2 parents 3bdcc90 + 85a1019 commit f56ffe9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ require 'vendor/autoload.php';
$api = new Binance\API("<api key>","<secret>");
```

#### 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("<api key>","<secret>", ['useServerTime'=>false], $proxyConf);
```

#### Get latest price of a symbol
```php
$ticker = $api->prices();
Expand Down
29 changes: 29 additions & 0 deletions examples/proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

require '../php-binance-api.php';

$proxyConf = [
'proto' => 'tcp',
'address' => '192.168.1.1',
'port' => '8080',
'user' => 'dude',
'pass' => 'd00d'
];

$api = new Binance\API("<key>","<secret>",["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;
54 changes: 51 additions & 3 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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" => [
Expand All @@ -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 {
Expand All @@ -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,'.','');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -267,7 +315,7 @@ private function balanceHandler($json) {
}
return $balances;
}

// Convert WebSocket ticker data into array
private function tickerStreamHandler($json) {
return [
Expand Down

0 comments on commit f56ffe9

Please sign in to comment.