From dd0a7460baa37c43acc5bfe50e16c176815eddab Mon Sep 17 00:00:00 2001 From: Esteban Garcia Date: Tue, 1 Jun 2021 15:10:12 +0100 Subject: [PATCH] implement PSR-3 logging --- EchoLogger.php | 17 +++++ composer.json | 3 +- php-binance-api.php | 168 ++++++++++++++++++++++++-------------------- 3 files changed, 112 insertions(+), 76 deletions(-) create mode 100644 EchoLogger.php diff --git a/EchoLogger.php b/EchoLogger.php new file mode 100644 index 00000000..25ad4009 --- /dev/null +++ b/EchoLogger.php @@ -0,0 +1,17 @@ +logger = $param[$i]; + array_splice($param, $i, 1); + break; + } + } + + if($this->logger == null) { + $this->logger = new EchoLogger(); + } + switch (count($param)) { case 0: $this->setupApiConfigFromFile(); @@ -98,7 +116,7 @@ public function __construct() $this->useTestnet = (bool)$param[2]; break; default: - echo 'Please see valid constructors here: https://github.com/jaggedsoft/php-binance-api/blob/master/examples/constructor.php'; + $this->logger->critical('Please see valid constructors here: https://github.com/jaggedsoft/php-binance-api/blob/master/examples/constructor.php'); } } @@ -143,8 +161,8 @@ protected function setupApiConfigFromFile(string $file = null) return; } if (file_exists($file) === false) { - echo "Unable to load config from: " . $file . PHP_EOL; - echo "Detected no API KEY or SECRET, all signed requests will fail" . PHP_EOL; + $this->logger->info("Unable to load config from: " . $file . PHP_EOL); + $this->logger->info("Detected no API KEY or SECRET, all signed requests will fail" . PHP_EOL); return; } $contents = json_decode(file_get_contents($file), true); @@ -169,8 +187,8 @@ protected function setupCurlOptsFromFile(string $file = null) return; } if (file_exists($file) === false) { - echo "Unable to load config from: " . $file . PHP_EOL; - echo "No curl options will be set" . PHP_EOL; + $this->logger->info("Unable to load config from: " . $file . PHP_EOL); + $this->logger->info("No curl options will be set" . PHP_EOL); return; } $contents = json_decode(file_get_contents($file), true); @@ -192,8 +210,8 @@ protected function setupProxyConfigFromFile(string $file = null) return; } if (file_exists($file) === false) { - echo "Unable to load config from: " . $file . PHP_EOL; - echo "No proxies will be used " . PHP_EOL; + $this->logger->info("Unable to load config from: " . $file . PHP_EOL); + $this->logger->info("No proxies will be used " . PHP_EOL); return; } $contents = json_decode(file_get_contents($file), true); @@ -376,8 +394,8 @@ public function marketBuyTest(string $symbol, $quantity, array $flags = []) { return $this->order("BUY", $symbol, $quantity, 0, "MARKET", $flags, true); } - - + + /** * numberOfDecimals() returns the signifcant digits level based on the minimum order amount. * @@ -633,15 +651,15 @@ public function exchangeInfo() { if (!$this->exchangeInfo) { $arr = $this->httpRequest("v3/exchangeInfo"); - + $this->exchangeInfo = $arr; $this->exchangeInfo['symbols'] = null; - + foreach ($arr['symbols'] as $key => $value) { $this->exchangeInfo['symbols'][$value['symbol']] = $value; } } - + return $this->exchangeInfo; } @@ -650,13 +668,13 @@ public function assetDetail() $params["wapi"] = true; return $this->httpRequest("v3/assetDetail.html", 'GET', $params, true); } - + public function userAssetDribbletLog() { $params["wapi"] = true; return $this->httpRequest("v3/userAssetDribbletLog.html", 'GET', $params, true); } - + /** * Fetch current(daily) trade fee of symbol, values in percentage. * for more info visit binance official api document @@ -671,7 +689,7 @@ public function tradeFee(string $symbol) "symbol" => $symbol, "wapi" => true, ]; - + return $this->httpRequest("v3/tradeFee.html", 'GET', $params, true); } @@ -887,7 +905,7 @@ public function aggTrades(string $symbol) "symbol" => $symbol, ])); } - + /** * historicalTrades - Get historical trades for a specific currency * @@ -941,7 +959,7 @@ public function depth(string $symbol, int $limit = 100) if (isset($symbol) === false || is_string($symbol) === false) { // WPCS: XSS OK. - echo "asset: expected bool false, " . gettype($symbol) . " given" . PHP_EOL; + $this->logger->info("asset: expected bool false, " . gettype($symbol) . " given" . PHP_EOL); } $json = $this->httpRequest("v1/depth", "GET", [ "symbol" => $symbol, @@ -972,11 +990,11 @@ public function balances($priceData = false) $account = $this->httpRequest("v3/account", "GET", [], true); if (is_array($account) === false) { - echo "Error: unable to fetch your account details" . PHP_EOL; + $this->logger->info("Error: unable to fetch your account details" . PHP_EOL); } if (isset($account['balances']) === false || empty($account['balances'])) { - echo "Error: your balances were empty or unset" . PHP_EOL; + $this->logger->info("Error: your balances were empty or unset" . PHP_EOL); return []; } @@ -1017,7 +1035,7 @@ public function getProxyUriString() if (in_array($uri, $supportedProtocols) === false) { // WPCS: XSS OK. - echo "Unknown proxy protocol '" . $this->proxyConf['proto'] . "', supported protocols are " . implode(", ", $supportedProtocols) . PHP_EOL; + $this->logger->info("Unknown proxy protocol '" . $this->proxyConf['proto'] . "', supported protocols are " . implode(", ", $supportedProtocols) . PHP_EOL); } $uri .= "://"; @@ -1025,7 +1043,7 @@ public function getProxyUriString() if (isset($this->proxyConf['address']) === false) { // WPCS: XSS OK. - echo "warning: proxy address not set defaulting to localhost" . PHP_EOL; + $this->logger->warning("warning: proxy address not set defaulting to localhost" . PHP_EOL); } $uri .= ":"; @@ -1033,7 +1051,7 @@ public function getProxyUriString() if (isset($this->proxyConf['address']) === false) { // WPCS: XSS OK. - echo "warning: proxy port not set defaulting to 1080" . PHP_EOL; + $this->logger->warning("warning: proxy port not set defaulting to 1080" . PHP_EOL); } return $uri; @@ -1111,7 +1129,7 @@ protected function httpRequest(string $url, string $method = "GET", array $param unset($params['wapi']); $base = $this->wapi; } - + if (isset($params['sapi'])) { if ($this->useTestnet) { throw new \Exception("sapi endpoints are not available in testnet"); @@ -1119,7 +1137,7 @@ protected function httpRequest(string $url, string $method = "GET", array $param unset($params['sapi']); $base = $this->sapi; } - + $query = http_build_query($params, '', '&'); $query = str_replace([ '%40' ], [ '@' ], $query);//if send data type "e-mail" then binance return: [Signature for this request is not valid.] $signature = hash_hmac('sha256', $query, $this->api_secret); @@ -1193,15 +1211,15 @@ protected function httpRequest(string $url, string $method = "GET", array $param // not outputing errors, hides it from users and ends up with tickets on github throw new \Exception('Curl error: ' . curl_error($curl)); } - + $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $header = substr($output, 0, $header_size); $output = substr($output, $header_size); - + curl_close($curl); - + $json = json_decode($output, true); - + $this->lastRequest = [ 'url' => $url, 'method' => $method, @@ -1268,12 +1286,12 @@ public function order(string $side, string $symbol, $quantity, $price, string $t if (is_numeric($quantity) === false) { // WPCS: XSS OK. - echo "warning: quantity expected numeric got " . gettype($quantity) . PHP_EOL; + $this->logger->warning("warning: quantity expected numeric got " . gettype($quantity) . PHP_EOL); } if (is_string($price) === false) { // WPCS: XSS OK. - echo "warning: price expected string got " . gettype($price) . PHP_EOL; + $this->logger->warning("warning: price expected string got " . gettype($price) . PHP_EOL); } if ($type === "LIMIT" || $type === "STOP_LOSS_LIMIT" || $type === "TAKE_PROFIT_LIMIT") { @@ -1346,7 +1364,7 @@ public function candlesticks(string $symbol, string $interval = "5m", int $limit } if (count($response) === 0) { - echo "warning: v1/klines returned empty array, usually a blip in the connection or server" . PHP_EOL; + $this->logger->warning("warning: v1/klines returned empty array, usually a blip in the connection or server" . PHP_EOL); return []; } @@ -1376,8 +1394,8 @@ protected function balanceData(array $array, $priceData) if (empty($array) || empty($array['balances'])) { // WPCS: XSS OK. - echo "balanceData error: Please make sure your system time is synchronized: call \$api->useServerTime() before this function" . PHP_EOL; - echo "ERROR: Invalid request. Please double check your API keys and permissions." . PHP_EOL; + $this->logger->error("balanceData error: Please make sure your system time is synchronized: call \$api->useServerTime() before this function" . PHP_EOL); + $this->logger->error("ERROR: Invalid request. Please double check your API keys and permissions." . PHP_EOL); return []; } @@ -2025,12 +2043,12 @@ public function depthCache($symbols, callable $callback) }); $ws->on('close', function ($code = null, $reason = null) use ($symbol, $loop) { // WPCS: XSS OK. - echo "depthCache({$symbol}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL; + $this->logger->info("depthCache({$symbol}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL); $loop->stop(); }); }, function ($e) use ($loop, $symbol) { // WPCS: XSS OK. - echo "depthCache({$symbol})) Could not connect: {$e->getMessage()}" . PHP_EOL; + $this->logger->info("depthCache({$symbol})) Could not connect: {$e->getMessage()}" . PHP_EOL); $loop->stop(); }); $this->depth($symbol, 100); @@ -2101,12 +2119,12 @@ public function trades($symbols, callable $callback) }); $ws->on('close', function ($code = null, $reason = null) use ($symbol, $loop) { // WPCS: XSS OK. - echo "trades({$symbol}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL; + $this->logger->info("trades({$symbol}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL); $loop->stop(); }); }, function ($e) use ($loop, $symbol) { // WPCS: XSS OK. - echo "trades({$symbol}) Could not connect: {$e->getMessage()}" . PHP_EOL; + $this->logger->info("trades({$symbol}) Could not connect: {$e->getMessage()}" . PHP_EOL); $loop->stop(); }); } @@ -2151,11 +2169,11 @@ public function ticker($symbol, callable $callback) }); $ws->on('close', function ($code = null, $reason = null) { // WPCS: XSS OK. - echo "ticker: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL; + $this->logger->info("ticker: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL); }); }, function ($e) { // WPCS: XSS OK. - echo "ticker: Could not connect: {$e->getMessage()}" . PHP_EOL; + $this->logger->info("ticker: Could not connect: {$e->getMessage()}" . PHP_EOL); }); // @codeCoverageIgnoreEnd } @@ -2232,7 +2250,7 @@ public function chart($symbols, string $interval = "30m", callable $callback = n }); }, function ($e) use ($loop, $symbol, $interval) { // WPCS: XSS OK. - echo "chart({$symbol},{$interval})) Could not connect: {$e->getMessage()}" . PHP_EOL; + $this->logger->info("chart({$symbol},{$interval})) Could not connect: {$e->getMessage()}" . PHP_EOL); $loop->stop(); }); $this->candlesticks($symbol, $interval, $limit); @@ -2290,12 +2308,12 @@ public function kline($symbols, string $interval = "30m", callable $callback = n }); $ws->on('close', function ($code = null, $reason = null) use ($symbol, $loop, $interval) { // WPCS: XSS OK. - echo "kline({$symbol},{$interval}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL; + $this->logger->info("kline({$symbol},{$interval}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL); $loop->stop(); }); }, function ($e) use ($loop, $symbol, $interval) { // WPCS: XSS OK. - echo "kline({$symbol},{$interval})) Could not connect: {$e->getMessage()}" . PHP_EOL; + $this->logger->info("kline({$symbol},{$interval})) Could not connect: {$e->getMessage()}" . PHP_EOL); $loop->stop(); }); } @@ -2409,11 +2427,11 @@ public function userData(&$balance_callback, &$execution_callback = false) }); $ws->on('close', function ($code = null, $reason = null) { // WPCS: XSS OK. - echo "userData: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL; + $this->logger->info("userData: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL); }); }, function ($e) { // WPCS: XSS OK. - echo "userData: Could not connect: {$e->getMessage()}" . PHP_EOL; + $this->logger->info("userData: Could not connect: {$e->getMessage()}" . PHP_EOL); }); $loop->run(); @@ -2461,11 +2479,11 @@ public function miniTicker(callable $callback) }); $ws->on('close', function ($code = null, $reason = null) { // WPCS: XSS OK. - echo "miniticker: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL; + $this->logger->info("miniticker: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL); }); }, function ($e) { // WPCS: XSS OK. - echo "miniticker: Could not connect: {$e->getMessage()}" . PHP_EOL; + $this->logger->info("miniticker: Could not connect: {$e->getMessage()}" . PHP_EOL); }); // @codeCoverageIgnoreEnd } @@ -2508,11 +2526,11 @@ public function bookTicker(callable $callback) }); $ws->on('close', function ($code = null, $reason = null) { // WPCS: XSS OK. - echo "miniticker: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL; + $this->logger->info("miniticker: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL); }); }, function ($e) { // WPCS: XSS OK. - echo "miniticker: Could not connect: {$e->getMessage()}" . PHP_EOL; + $this->logger->info("miniticker: Could not connect: {$e->getMessage()}" . PHP_EOL); }); // @codeCoverageIgnoreEnd } @@ -2551,21 +2569,21 @@ protected function downloadCurlCaBundle() curl_close($curl); if ($result === false) { - echo "Unable to to download the CA bundle $host" . PHP_EOL; + $this->logger->error("Unable to to download the CA bundle $host" . PHP_EOL); return; } $fp = fopen($output_filename, 'w'); if ($fp === false) { - echo "Unable to write $output_filename, please check permissions on folder" . PHP_EOL; + $this->logger->error("Unable to write $output_filename, please check permissions on folder" . PHP_EOL); return; } fwrite($fp, $result); fclose($fp); } - + protected function floorDecimal($n, $decimals=2) { return floor($n * pow(10, $decimals)) / pow(10, $decimals); @@ -2610,11 +2628,11 @@ public function isOnTestnet() : bool /** * systemStatus - Status indicator for sapi and wapi * 0 = Normal, 1 = System Maintenance - * + * * @link https://binance-docs.github.io/apidocs/spot/en/#system-status-system - * + * * @property int $weight 1 - * + * * @return array containing the response * @throws \Exception */ @@ -2623,28 +2641,28 @@ public function systemStatus() $arr = array(); $api_status = $this->httpRequest("v3/ping", 'GET'); if ( empty($api_status) ) { - $arr['api']['status'] = 'ping ok'; + $arr['api']['status'] = 'ping ok'; } else { - $arr['api']['status'] = $api_status; + $arr['api']['status'] = $api_status; } - + $arr['sapi'] = $this->httpRequest("v1/system/status", 'GET', [ 'sapi' => true ], true); $arr['wapi'] = $this->httpRequest("v3/systemStatus.html", 'GET', [ 'wapi' => true ], true); return $arr; } - + /** * accountSnapshot - Daily Account Snapshot at 00:00:00 UTC - * + * * @link https://binance-docs.github.io/apidocs/spot/en/#daily-account-snapshot-user_data - * + * * @property int $weight 1 - * + * * @param string $type (mandatory) Should be SPOT, MARGIN or FUTURES * @param int $nbrDays (optional) Number of days. Default 5, min 5, max 30 * @param long $startTime (optional) Start time, e.g. 1617580799000 * @param long $endTime (optional) End time, e.g. 1617667199000 - * + * * @return array containing the response * @throws \Exception */ @@ -2652,30 +2670,30 @@ public function accountSnapshot($type, $nbrDays = 5, $startTime = 0, $endTime = { if ($nbrDays < 5 || $nbrDays > 30) $nbrDays = 5; - + $params = [ 'sapi' => true, 'type' => $type, ]; - + if ($startTime > 0) $params['startTime'] = $startTime; if ($endTime > 0) $params['endTime'] = $startTime; if ($nbrDays != 5) $params['limit'] = $limit; - + return $this->httpRequest("v1/accountSnapshot", 'GET', $params, true); } - + /** * accountStatus - Fetch account status detail. - * + * * @link https://binance-docs.github.io/apidocs/spot/en/#account-status-user_data * @link https://binance-docs.github.io/apidocs/spot/en/#account-status-sapi-user_data - * + * * @property int $weight 2 - * + * * @return array containing the response * @throws \Exception */ @@ -2686,15 +2704,15 @@ public function accountStatus() $arr['wapi'] = $this->httpRequest("v3/accountStatus.html", 'GET', [ 'wapi' => true ], true); return $arr; } - + /** * apiTradingStatus - Fetch account API trading status detail. - * + * * @link https://binance-docs.github.io/apidocs/spot/en/#account-api-trading-status-user_data * @link https://binance-docs.github.io/apidocs/spot/en/#account-api-trading-status-sapi-user_data - * + * * @property int $weight 2 - * + * * @return array containing the response * @throws \Exception */ @@ -2704,5 +2722,5 @@ public function apiTradingStatus() $arr['sapi'] = $this->httpRequest("v1/account/apiTradingStatus", 'GET', [ 'sapi' => true ], true); $arr['wapi'] = $this->httpRequest("v3/apiTradingStatus.html", 'GET', [ 'wapi' => true ], true); return $arr; - } + } }