From 921e5522b920947cca206ad5eae2a72a1c3f1dc1 Mon Sep 17 00:00:00 2001 From: Xibel Date: Mon, 28 Oct 2019 17:36:28 +0100 Subject: [PATCH 1/5] Replaced request exception with Laravel log facade --- src/BinanceAPI.php | 48 +++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/BinanceAPI.php b/src/BinanceAPI.php index b261005..a531be0 100755 --- a/src/BinanceAPI.php +++ b/src/BinanceAPI.php @@ -1,6 +1,8 @@ curl); - if($result === false) - throw new \Exception('CURL error: ' . curl_error($this->curl)); - - // decode results - $result = json_decode($result, true); - - if(!is_array($result) || json_last_error()) - throw new \Exception('JSON decode error'); + if($result === false) { + Log::Error('BinanceAPI: CURL not completed. No network? Error: '.curl_error($this->curl)); + } else { + // decode results + $result = json_decode($result, true); + if(!is_array($result) || json_last_error()) + Log::Error('BinanceAPI: JSON decode error in Binance API'); + } return $result; @@ -358,13 +360,14 @@ private function privateRequest($url, $params = [], $method = 'GET') //Get result $result = curl_exec($this->curl); - if($result === false) - throw new \Exception('CURL error: ' . curl_error($this->curl)); - - // decode results - $result = json_decode($result, true); - if(!is_array($result) || json_last_error()) - throw new \Exception('JSON decode error'); + if($result === false) { + Log::Error('BinanceAPI: CURL not completed. No network? Error: '.curl_error($this->curl)); + } else { + // decode results + $result = json_decode($result, true); + if(!is_array($result) || json_last_error()) + Log::Error('BinanceAPI: JSON decode error in Binance API'); + } return $result; @@ -411,13 +414,14 @@ private function wapiRequest($url, $params = [], $method = 'GET') //Get result $result = curl_exec($this->curl); - if($result === false) - throw new \Exception('CURL error: ' . curl_error($this->curl)); - - // decode results - $result = json_decode($result, true); - if(!is_array($result) || json_last_error()) - throw new \Exception('JSON decode error'); + if($result === false) { + Log::Error('BinanceAPI: CURL not completed. No network? Error: '.curl_error($this->curl)); + } else { + // decode results + $result = json_decode($result, true); + if(!is_array($result) || json_last_error()) + Log::Error('BinanceAPI: JSON decode error in Binance API'); + } return $result; From efd7238dcd53a873dc1e6693868492d6d723d7b3 Mon Sep 17 00:00:00 2001 From: Xibel Date: Tue, 29 Oct 2019 10:01:05 +0100 Subject: [PATCH 2/5] Define timeouts in Binance config --- config/binance.php | 4 +++- src/BinanceAPI.php | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/config/binance.php b/config/binance.php index 43a431f..d3890ec 100644 --- a/config/binance.php +++ b/config/binance.php @@ -40,7 +40,9 @@ 'settings' => [ 'timing' => env('BINANCE_TIMING', 5000), - 'ssl' => env('BINANCE_SSL_VERIFYPEER', true) + 'ssl' => env('BINANCE_SSL_VERIFYPEER', true), + 'connectiontimeout' => env('BINANCE_SETTINGS_CONNECTIONTIMEOUT', 20), + 'timeout' => env('BINANCE_SETTINGS_TIMEOUT', 300) ], ]; diff --git a/src/BinanceAPI.php b/src/BinanceAPI.php index a531be0..59ca6ec 100755 --- a/src/BinanceAPI.php +++ b/src/BinanceAPI.php @@ -29,8 +29,8 @@ function __construct() CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_USERAGENT => 'Binance PHP API Agent', CURLOPT_RETURNTRANSFER => true, - CURLOPT_CONNECTTIMEOUT => 20, - CURLOPT_TIMEOUT => 300 + CURLOPT_CONNECTTIMEOUT => config('binance.settings.connectiontimeout'), + CURLOPT_TIMEOUT => config('binance.settings.timeout') ]; curl_setopt_array($this->curl, $curl_options); From a67d17fd901a4e9cc9817a5219bd77e5bb5024f3 Mon Sep 17 00:00:00 2001 From: Xibel Date: Tue, 29 Oct 2019 10:05:49 +0100 Subject: [PATCH 3/5] Added publish command --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 4396920..d2a57df 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,12 @@ Utilises autoloading in Laravel 5.5+. For older versions add the following lines ], ``` +#### Publish config + +``` +php artisan vendor:publish --provider="adman9000\binance\BinanceServiceProvider" +``` + ## Features Price tickers, balances, trades From 2ea0380851e06b790df7c747c27f7389e9d68fb3 Mon Sep 17 00:00:00 2001 From: Xibel Date: Tue, 29 Oct 2019 10:35:20 +0100 Subject: [PATCH 4/5] Return null if curl error --- src/BinanceAPI.php | 47 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/BinanceAPI.php b/src/BinanceAPI.php index 59ca6ec..151b719 100755 --- a/src/BinanceAPI.php +++ b/src/BinanceAPI.php @@ -1,8 +1,6 @@ curl); - if($result === false) { - Log::Error('BinanceAPI: CURL not completed. No network? Error: '.curl_error($this->curl)); - } else { - // decode results - $result = json_decode($result, true); - if(!is_array($result) || json_last_error()) - Log::Error('BinanceAPI: JSON decode error in Binance API'); - } + if($result === false) + return null; + + // decode results + $result = json_decode($result, true); + if(!is_array($result) || json_last_error()) + return null; return $result; @@ -360,14 +357,13 @@ private function privateRequest($url, $params = [], $method = 'GET') //Get result $result = curl_exec($this->curl); - if($result === false) { - Log::Error('BinanceAPI: CURL not completed. No network? Error: '.curl_error($this->curl)); - } else { - // decode results - $result = json_decode($result, true); - if(!is_array($result) || json_last_error()) - Log::Error('BinanceAPI: JSON decode error in Binance API'); - } + if($result === false) + return null; + + // decode results + $result = json_decode($result, true); + if(!is_array($result) || json_last_error()) + return null; return $result; @@ -414,14 +410,13 @@ private function wapiRequest($url, $params = [], $method = 'GET') //Get result $result = curl_exec($this->curl); - if($result === false) { - Log::Error('BinanceAPI: CURL not completed. No network? Error: '.curl_error($this->curl)); - } else { - // decode results - $result = json_decode($result, true); - if(!is_array($result) || json_last_error()) - Log::Error('BinanceAPI: JSON decode error in Binance API'); - } + if($result === false) + return null; + + // decode results + $result = json_decode($result, true); + if(!is_array($result) || json_last_error()) + return null; return $result; From 97d6bf092e30dbab3616820049f16fe98db66bb3 Mon Sep 17 00:00:00 2001 From: Xibel Date: Tue, 29 Oct 2019 11:31:14 +0100 Subject: [PATCH 5/5] Changed Readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d2a57df..2fe3e14 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ Utilises autoloading in Laravel 5.5+. For older versions add the following lines #### Publish config +Create a configurable config file: + ``` php artisan vendor:publish --provider="adman9000\binance\BinanceServiceProvider" ```