From b027f309b4500d88a5dca02a4decc97c84e04907 Mon Sep 17 00:00:00 2001 From: ePascalC Date: Mon, 16 Aug 2021 12:32:48 +0200 Subject: [PATCH 1/3] Enable http_build_query to handle arrays The standard http_build_query cannot handle queries, but some Binance features like sapi v1/asset/dust can have an array. --- php-binance-api.php | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/php-binance-api.php b/php-binance-api.php index 28228f2e..d13e9cb1 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -1222,7 +1222,7 @@ protected function httpRequest(string $url, string $method = "GET", array $param $curl = curl_init(); curl_setopt($curl, CURLOPT_VERBOSE, $this->httpDebug); - $query = http_build_query($params, '', '&'); + $query = $this->binance_build_query($params, '', '&'); // signed with params if ($signed === true) { @@ -1253,13 +1253,13 @@ protected function httpRequest(string $url, string $method = "GET", array $param $base = $this->sapi; } - $query = http_build_query($params, '', '&'); + $query = $this->binance_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); if ($method === "POST") { $endpoint = $base . $url; $params['signature'] = $signature; // signature needs to be inside BODY - $query = http_build_query($params, '', '&'); // rebuilding query + $query = $this->binance_build_query($params, '', '&'); // rebuilding query } else { $endpoint = $base . $url . '?' . $query . '&signature=' . $signature; } @@ -1363,6 +1363,35 @@ protected function httpRequest(string $url, string $method = "GET", array $param return $json; } + /** + * binance_build_query - Wrapper for http_build_query to allow arrays as parameters + * + * sapi v1/asset/dust can have an array, so it needs a conversion + * + * @param array $params (mandatory) Parameters to convert to http query + * + * @return array containing the response + * @throws \Exception + */ + protected function binance_build_query($params = []) + { + $new_arr = array(); + $query_add = ''; + foreach ($params as $label=>$item) { + if ( gettype($item) == 'array' ) { + foreach ($item as $arritem) { + $query_add = $label . '=' . $arritem . '&' . $query_add; + } + } else { + $new_arr[$label] = $item; + } + } + $query = http_build_query($new_arr, '', '&'); + $query = $query_add . $query; + + return $query; + } + /** * Converts the output of the CURL header to an array * From 1004aa14e33a8ccf5f09ff3ae267e302b50c6c18 Mon Sep 17 00:00:00 2001 From: ePascalC Date: Mon, 16 Aug 2021 13:29:52 +0200 Subject: [PATCH 2/3] Update php-binance-api.php --- php-binance-api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php-binance-api.php b/php-binance-api.php index d13e9cb1..c612a0f6 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -1253,13 +1253,13 @@ protected function httpRequest(string $url, string $method = "GET", array $param $base = $this->sapi; } - $query = $this->binance_build_query($params, '', '&'); + $query = $this->binance_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); if ($method === "POST") { $endpoint = $base . $url; $params['signature'] = $signature; // signature needs to be inside BODY - $query = $this->binance_build_query($params, '', '&'); // rebuilding query + $query = $this->binance_build_query($params); // rebuilding query } else { $endpoint = $base . $url . '?' . $query . '&signature=' . $signature; } From 16a4cca6111b2c1c553a765f7a24c15e14a9dc57 Mon Sep 17 00:00:00 2001 From: ePascalC Date: Mon, 16 Aug 2021 13:31:13 +0200 Subject: [PATCH 3/3] Update php-binance-api.php --- php-binance-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php-binance-api.php b/php-binance-api.php index c612a0f6..ce4a1792 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -1222,7 +1222,7 @@ protected function httpRequest(string $url, string $method = "GET", array $param $curl = curl_init(); curl_setopt($curl, CURLOPT_VERBOSE, $this->httpDebug); - $query = $this->binance_build_query($params, '', '&'); + $query = $this->binance_build_query($params); // signed with params if ($signed === true) {