Skip to content

Commit a6b0aef

Browse files
chrisblackwelldmzoneill
authored andcommitted
Set to null but throw Exception if it is null
1 parent bff82d3 commit a6b0aef

1 file changed

Lines changed: 67 additions & 51 deletions

File tree

php-binance-api.php

Lines changed: 67 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
namespace Binance;
1414

15+
use Exception;
16+
1517
// PHP version check
1618
if (version_compare(phpversion(), '7.0', '<=')) {
1719
fwrite(STDERR, "Hi, PHP " . phpversion() . " support will be removed very soon as part of continued development.\n");
@@ -55,7 +57,7 @@ class API
5557

5658
// /< value of available onOrder assets
5759

58-
protected $exchangeInfo = NULL;
60+
protected $exchangeInfo = null;
5961
protected $lastRequest = [];
6062

6163
protected $xMbxUsedWeight = 0;
@@ -374,8 +376,8 @@ public function marketBuyTest(string $symbol, $quantity, array $flags = [])
374376
{
375377
return $this->order("BUY", $symbol, $quantity, 0, "MARKET", $flags, true);
376378
}
377-
378-
379+
380+
379381
/**
380382
* numberOfDecimals() returns the signifcant digits level based on the minimum order amount.
381383
*
@@ -384,9 +386,10 @@ public function marketBuyTest(string $symbol, $quantity, array $flags = [])
384386
* @param $val float the minimum order amount for the pair
385387
* @return integer (signifcant digits) based on the minimum order amount
386388
*/
387-
public function numberOfDecimals($val = 0.00000001){
389+
public function numberOfDecimals($val = 0.00000001)
390+
{
388391
$val = sprintf("%.14f", $val);
389-
$parts = explode('.', $val);
392+
$parts = explode('.', $val);
390393
$parts[1] = rtrim($parts[1], "0");
391394
return strlen($parts[1]);
392395
}
@@ -553,10 +556,13 @@ public function cancelOpenOrders(string $symbol = null)
553556
* @return array with error message or array of orderDetails array
554557
* @throws \Exception
555558
*/
556-
public function orders(string $symbol, int $limit = 500, int $fromOrderId = 0, array $params = []) {
557-
$params["symbol"] = $symbol;
558-
$params["limit"] = $limit;
559-
if ( $fromOrderId ) $params["orderId"] = $fromOrderId;
559+
public function orders(string $symbol, int $limit = 500, int $fromOrderId = 0, array $params = [])
560+
{
561+
$params["symbol"] = $symbol;
562+
$params["limit"] = $limit;
563+
if ($fromOrderId) {
564+
$params["orderId"] = $fromOrderId;
565+
}
560566
return $this->httpRequest("v3/allOrders", "GET", $params, true);
561567
}
562568

@@ -625,17 +631,15 @@ public function time()
625631
*/
626632
public function exchangeInfo()
627633
{
628-
if(!$this->exchangeInfo){
629-
634+
if (!$this->exchangeInfo) {
630635
$arr = $this->httpRequest("v1/exchangeInfo");
631636

632637
$this->exchangeInfo = $arr;
633638
$this->exchangeInfo['symbols'] = null;
634639

635-
foreach($arr['symbols'] as $key => $value){
640+
foreach ($arr['symbols'] as $key => $value) {
636641
$this->exchangeInfo['symbols'][$value['symbol']] = $value;
637642
}
638-
639643
}
640644

641645
return $this->exchangeInfo;
@@ -646,13 +650,13 @@ public function assetDetail()
646650
$params["wapi"] = true;
647651
return $this->httpRequest("v3/assetDetail.html", 'GET', $params, true);
648652
}
649-
653+
650654
public function userAssetDribbletLog()
651655
{
652656
$params["wapi"] = true;
653657
return $this->httpRequest("v3/userAssetDribbletLog.html", 'GET', $params, true);
654658
}
655-
659+
656660
/**
657661
* Fetch current(daily) trade fee of symbol, values in percentage.
658662
* for more info visit binance official api document
@@ -663,11 +667,11 @@ public function userAssetDribbletLog()
663667
*/
664668
public function tradeFee(string $symbol)
665669
{
666-
$params = [
670+
$params = [
667671
"symbol" => $symbol,
668672
"wapi" => true,
669673
];
670-
674+
671675
return $this->httpRequest("v3/tradeFee.html", 'GET', $params, true);
672676
}
673677

@@ -691,7 +695,7 @@ public function tradeFee(string $symbol)
691695
* @return array with error message or array transaction
692696
* @throws \Exception
693697
*/
694-
public function withdraw(string $asset, string $address, $amount, $addressTag = null, $addressName = "", bool $transactionFeeFlag = false,$network = null)
698+
public function withdraw(string $asset, string $address, $amount, $addressTag = null, $addressName = "", bool $transactionFeeFlag = false, $network = null)
695699
{
696700
$options = [
697701
"asset" => $asset,
@@ -883,7 +887,7 @@ public function aggTrades(string $symbol)
883887
"symbol" => $symbol,
884888
]));
885889
}
886-
890+
887891
/**
888892
* historicalTrades Get historical trades for a specific currency
889893
*
@@ -908,7 +912,7 @@ public function historicalTrades(string $symbol, int $limit = 500, int $fromTrad
908912
}
909913

910914
// The endpoint cannot handle extra parameters like 'timestamp' or 'signature',
911-
// but it needs the http header with the key so we need to construct it here
915+
// but it needs the http header with the key so we need to construct it here
912916
$query = http_build_query($parameters, '', '&');
913917
return $this->httpRequest("v3/historicalTrades?$query");
914918
}
@@ -967,7 +971,7 @@ public function balances($priceData = false)
967971

968972
if (isset($account['balances']) === false || empty($account['balances'])) {
969973
echo "Error: your balances were empty or unset" . PHP_EOL;
970-
return [];
974+
return [];
971975
}
972976

973977
return $this->balanceData($account, $priceData);
@@ -980,7 +984,8 @@ public function balances($priceData = false)
980984
* @return array with error message or array containing coins
981985
* @throws \Exception
982986
*/
983-
public function coins(){
987+
public function coins()
988+
{
984989
return $this->httpRequest('v1/capital/config/getall', 'GET', [ 'sapi' => true ], true);
985990
}
986991

@@ -1100,21 +1105,21 @@ protected function httpRequest(string $url, string $method = "GET", array $param
11001105
unset($params['wapi']);
11011106
$base = $this->wapi;
11021107
}
1103-
1108+
11041109
if (isset($params['sapi'])) {
11051110
if ($this->useTestnet) {
11061111
throw new \Exception("sapi endpoints are not available in testnet");
11071112
}
11081113
unset($params['sapi']);
11091114
$base = $this->sapi;
11101115
}
1111-
1116+
11121117
$query = http_build_query($params, '', '&');
11131118
$signature = hash_hmac('sha256', $query, $this->api_secret);
11141119
if ($method === "POST") {
11151120
$endpoint = $base . $url;
1116-
$params['signature'] = $signature; // signature needs to be inside BODY
1117-
$query = http_build_query($params, '', '&'); // rebuilding query
1121+
$params['signature'] = $signature; // signature needs to be inside BODY
1122+
$query = http_build_query($params, '', '&'); // rebuilding query
11181123
} else {
11191124
$endpoint = $base . $url . '?' . $query . '&signature=' . $signature;
11201125
}
@@ -1125,7 +1130,7 @@ protected function httpRequest(string $url, string $method = "GET", array $param
11251130
));
11261131
}
11271132
// params so buildquery string and append to url
1128-
else if (count($params) > 0) {
1133+
elseif (count($params) > 0) {
11291134
curl_setopt($curl, CURLOPT_URL, $this->getRestEndpoint() . $url . '?' . $query);
11301135
}
11311136
// no params so just the base url
@@ -1206,7 +1211,7 @@ protected function httpRequest(string $url, string $method = "GET", array $param
12061211
$this->setXMbxUsedWeight1m($header['x-mbx-used-weight-1m']);
12071212
}
12081213

1209-
if(isset($json['msg']) && !empty($json['msg'])){
1214+
if (isset($json['msg']) && !empty($json['msg'])) {
12101215
// should always output error, not only on httpdebug
12111216
// not outputing errors, hides it from users and ends up with tickets on github
12121217
throw new \Exception('signedRequest error: '.print_r($output, true));
@@ -1390,7 +1395,7 @@ protected function balanceData(array $array, $priceData)
13901395
$btc_value += $obj['free'];
13911396
$btc_total += $obj['free'] + $obj['locked'];
13921397
continue;
1393-
} elseif ( $asset === 'USDT' || $asset === 'USDC' || $asset === 'PAX' || $asset === 'BUSD' ) {
1398+
} elseif ($asset === 'USDT' || $asset === 'USDC' || $asset === 'PAX' || $asset === 'BUSD') {
13941399
$btcValue = $obj['free'] / $priceData['BTCUSDT'];
13951400
$btcTotal = ($obj['free'] + $obj['locked']) / $priceData['BTCUSDT'];
13961401
$balances[$asset]['btcValue'] = $btcValue;
@@ -1871,14 +1876,12 @@ protected function depthHandler(array $json)
18711876
if ($bid[1] == "0.00000000") {
18721877
unset($this->depthCache[$symbol]['bids'][$bid[0]]);
18731878
}
1874-
18751879
}
18761880
foreach ($json['a'] as $ask) {
18771881
$this->depthCache[$symbol]['asks'][$ask[0]] = $ask[1];
18781882
if ($ask[1] == "0.00000000") {
18791883
unset($this->depthCache[$symbol]['asks'][$ask[0]]);
18801884
}
1881-
18821885
}
18831886
}
18841887

@@ -2161,8 +2164,11 @@ public function ticker($symbol, callable $callback)
21612164
* @return null
21622165
* @throws \Exception
21632166
*/
2164-
public function chart($symbols, string $interval = "30m", callable $callback, $limit = 500)
2167+
public function chart($symbols, string $interval = "30m", callable $callback = null, $limit = 500)
21652168
{
2169+
if (is_null($callback)) {
2170+
throw new Exception("You must provide a valid callback");
2171+
}
21662172
if (!is_array($symbols)) {
21672173
$symbols = [
21682174
$symbols,
@@ -2242,8 +2248,11 @@ public function chart($symbols, string $interval = "30m", callable $callback, $l
22422248
* @return null
22432249
* @throws \Exception
22442250
*/
2245-
public function kline($symbols, string $interval = "30m", callable $callback)
2251+
public function kline($symbols, string $interval = "30m", callable $callback = null)
22462252
{
2253+
if (is_null($callback)) {
2254+
throw new Exception("You must provide a valid callback");
2255+
}
22472256
if (!is_array($symbols)) {
22482257
$symbols = [
22492258
$symbols,
@@ -2450,7 +2459,7 @@ public function miniTicker(callable $callback)
24502459
// @codeCoverageIgnoreEnd
24512460
}
24522461

2453-
/**
2462+
/**
24542463
* bookTicker Get bookTicker for all symbols
24552464
*
24562465
* $api->bookTicker(function($api, $ticker) {
@@ -2460,7 +2469,7 @@ public function miniTicker(callable $callback)
24602469
* @param $callback callable function closer that takes 2 arguments, $api and $ticker data
24612470
* @return null
24622471
*/
2463-
public function bookTicker(callable $callback)
2472+
public function bookTicker(callable $callback)
24642473
{
24652474
$endpoint = '!bookticker';
24662475
$this->subscriptions[$endpoint] = true;
@@ -2476,14 +2485,14 @@ public function bookTicker(callable $callback)
24762485
}
24772486
$json = json_decode($data, true);
24782487

2479-
$markets = [
2480-
"updateId" => $json['u'],
2481-
"symbol" => $json['s'],
2482-
"bid_price" => $json['b'],
2483-
"bid_qty" => $json['B'],
2484-
"ask_price" => $json['a'],
2485-
"ask_qty" => $json['A'],
2486-
];
2488+
$markets = [
2489+
"updateId" => $json['u'],
2490+
"symbol" => $json['s'],
2491+
"bid_price" => $json['b'],
2492+
"bid_qty" => $json['B'],
2493+
"ask_price" => $json['a'],
2494+
"ask_qty" => $json['A'],
2495+
];
24872496
call_user_func($callback, $this, $markets);
24882497
});
24892498
$ws->on('close', function ($code = null, $reason = null) {
@@ -2547,36 +2556,43 @@ protected function downloadCurlCaBundle()
25472556
}
25482557

25492558
protected function floorDecimal($n, $decimals=2)
2550-
{
2559+
{
25512560
return floor($n * pow(10, $decimals)) / pow(10, $decimals);
25522561
}
25532562

25542563

2555-
protected function setXMbxUsedWeight (int $usedWeight) : void {
2564+
protected function setXMbxUsedWeight(int $usedWeight) : void
2565+
{
25562566
$this->xMbxUsedWeight = $usedWeight;
25572567
}
25582568

2559-
protected function setXMbxUsedWeight1m (int $usedWeight1m) : void {
2569+
protected function setXMbxUsedWeight1m(int $usedWeight1m) : void
2570+
{
25602571
$this->xMbxUsedWeight1m = $usedWeight1m;
25612572
}
25622573

2563-
public function getXMbxUsedWeight () : int {
2574+
public function getXMbxUsedWeight() : int
2575+
{
25642576
$this->xMbxUsedWeight;
25652577
}
25662578

2567-
public function getXMbxUsedWeight1m () : int {
2579+
public function getXMbxUsedWeight1m() : int
2580+
{
25682581
$this->xMbxUsedWeight1m;
25692582
}
25702583

2571-
private function getRestEndpoint() : string {
2584+
private function getRestEndpoint() : string
2585+
{
25722586
return $this->useTestnet ? $this->baseTestnet : $this->base;
25732587
}
25742588

2575-
private function getWsEndpoint() : string {
2589+
private function getWsEndpoint() : string
2590+
{
25762591
return $this->useTestnet ? $this->streamTestnet : $this->stream;
25772592
}
25782593

2579-
public function isOnTestnet() : bool {
2594+
public function isOnTestnet() : bool
2595+
{
25802596
return $this->useTestnet;
25812597
}
25822598
}

0 commit comments

Comments
 (0)