Current path: home/webdevt/cryptoimpot.fr/module/Application/src/ExchangeAPI/
?? Go up: /home/webdevt/cryptoimpot.fr/module/Application/src
<?php
namespace Application\ExchangeAPI;
/**
* Description of BinanceAPI
*
* https://github.com/jaggedsoft/php-binance-api/blob/master/php-binance-api.php
*/
class BinanceAPI extends \Binance\API {
/**
* Due to ongoing issues with out of date wamp CA bundles
* This function downloads ca bundle for curl website
* and uses it as part of the curl options
*/
private function downloadCurlCaBundle() {
// var_dump(getcwd()); exit;
//$output_filename = getcwd() . "/data/io/ca.pem";
// if (is_writable(getcwd()) === false) {
// die(getcwd() . " folder is not writeable, plese check your permissions");
// }
//$host = "https://curl.haxx.se/ca/cacert.pem";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $host);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
// proxy settings
if (is_array($this->proxyConf)) {
curl_setopt($curl, CURLOPT_PROXY, $this->getProxyUriString());
if (isset($this->proxyConf['user']) && isset($this->proxyConf['pass'])) {
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->proxyConf['user'] . ':' . $this->proxyConf['pass']);
}
}
$result = curl_exec($curl);
curl_close($curl);
if ($result === false) {
echo "Unable to to download the CA bundle $host" . PHP_EOL;
return;
}
// var_dump($result);
// exit;
$fp = fopen($output_filename, 'w');
if ($fp === false) {
echo "Unable to write $output_filename, please check permissions on folder" . PHP_EOL;
return;
}
fwrite($fp, $result);
fclose($fp);
}
private function balanceData(array $array, $priceData)
{
$balances = [];
if (is_array($priceData)) {
$btc_value = $btc_total = 0.00;
}
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;
return [];
}
foreach ($array['balances'] as $obj) {
$asset = $obj['asset'];
$balances[$asset] = [
"available" => $obj['free'],
"onOrder" => $obj['locked'],
"btcValue" => 0.00000000,
"btcTotal" => 0.00000000,
];
if (is_array($priceData) === false) {
continue;
}
if ($obj['free'] + $obj['locked'] < 0.00000001) {
continue;
}
if ($asset === 'BTC') {
$balances[$asset]['btcValue'] = $obj['free'];
$balances[$asset]['btcTotal'] = $obj['free'] + $obj['locked'];
$btc_value += $obj['free'];
$btc_total += $obj['free'] + $obj['locked'];
continue;
}
$symbol = $asset . 'BTC';
if ($symbol === 'BTCUSDT') {
$btcValue = number_format($obj['free'] / $priceData['BTCUSDT'], 8, '.', '');
$btcTotal = number_format(($obj['free'] + $obj['locked']) / $priceData['BTCUSDT'], 8, '.', '');
} elseif (isset($priceData[$symbol]) === false) {
$btcValue = $btcTotal = 0;
} else {
$btcValue = number_format($obj['free'] * $priceData[$symbol], 8, '.', '');
$btcTotal = number_format(($obj['free'] + $obj['locked']) * $priceData[$symbol], 8, '.', '');
}
$balances[$asset]['btcValue'] = $btcValue;
$balances[$asset]['btcTotal'] = $btcTotal;
$btc_value += $btcValue;
$btc_total += $btcTotal;
}
if (is_array($priceData)) {
uasort($balances, function ($opA, $opB) {
return $opA['btcValue'] < $opB['btcValue'];
});
$this->btc_value = $btc_value;
$this->btc_total = $btc_total;
}
return $balances;
}
public function exchangeInfo()
{
return $this->httpRequest("v1/exchangeInfo");
}
public function history(string $symbol = '', int $limit = 500, int $fromTradeId = -1) {
$parameters = [
"symbol" => $symbol,
// 'recvWindow'=> 10000000
'recvWindow'=> 60000,
"limit" => $limit
];
if ($fromTradeId > 0) {
$parameters["fromId"] = $fromTradeId;
}
return $this->httpRequest("v3/myTrades", "GET", $parameters, true);
}
public function balances($priceData = false)
{
if (is_array($priceData) === false) {
$priceData = false;
}
$account = $this->httpRequest("v3/account", "GET", [], true);
if (is_array($account) === false) {
echo "Error: unable to fetch your account details" . PHP_EOL;
}
if (isset($account['balances']) === false) {
echo "Error: your balances were empty or unset" . PHP_EOL;
}
return $this->balanceData($account, $priceData);
}
private function httpRequest(string $url, string $method = "GET", array $params = [], bool $signed = false)
{
if (function_exists('curl_init') === false) {
throw new \Exception("Sorry cURL is not installed!");
}
if ($this->caOverride === false) {
if (file_exists(getcwd() . '/ca.pem') === false) {
//$this->downloadCurlCaBundle();
}
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, $this->httpDebug);
$query = http_build_query($params, '', '&');
// signed with params
if ($signed === true) {
if (empty($this->api_key)) {
throw new \Exception("signedRequest error: API Key not set!");
}
if (empty($this->api_secret)) {
throw new \Exception("signedRequest error: API Secret not set!");
}
$base = $this->base;
$ts = (microtime(true) * 1000) + $this->info['timeOffset'];
$params['timestamp'] = number_format($ts, 0, '.', '');
if (isset($params['wapi'])) {
unset($params['wapi']);
$base = $this->wapi;
}
$query = http_build_query($params, '', '&');
$signature = hash_hmac('sha256', $query, $this->api_secret);
if ($method === "POST") {
$endpoint = $base . $url . '?' . 'signature=' . $signature;
} else
$endpoint = $base . $url . '?' . $query . '&signature=' . $signature;
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-MBX-APIKEY: ' . $this->api_key,
));
}
// params so buildquery string and append to url
else if (count($params) > 0) {
curl_setopt($curl, CURLOPT_URL, $this->base . $url . '?' . $query);
}
// no params so just the base url
else {
curl_setopt($curl, CURLOPT_URL, $this->base . $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-MBX-APIKEY: ' . $this->api_key,
));
}
curl_setopt($curl, CURLOPT_USERAGENT, "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)");
// Post and postfields
if ($method === "POST") {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $query);
}
// Delete Method
if ($method === "DELETE") {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
}
// PUT Method
if ($method === "PUT") {
curl_setopt($curl, CURLOPT_PUT, true);
}
// proxy settings
if (is_array($this->proxyConf)) {
curl_setopt($curl, CURLOPT_PROXY, $this->getProxyUriString());
if (isset($this->proxyConf['user']) && isset($this->proxyConf['pass'])) {
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->proxyConf['user'] . ':' . $this->proxyConf['pass']);
}
}
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// headers will proceed the output, json_decode will fail below
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
// set user defined curl opts last for overriding
foreach ($this->curlOpts as $key => $value) {
curl_setopt($curl, constant($key), $value);
}
if ($this->caOverride === false) {
if (file_exists(getcwd() . '/ca.pem') === false) {
//$this->downloadCurlCaBundle();
}
}
$output = curl_exec($curl);
// Check if any error occurred
if (curl_errno($curl) > 0) {
// should always output error, not only on httpdebug
// not outputing errors, hides it from users and ends up with tickets on github
echo 'Curl error: ' . curl_error($curl) . "\n";
return [];
}
curl_close($curl);
$json = json_decode($output, true);
if (isset($json['msg'])) {
// should always output error, not only on httpdebug
// not outputing errors, hides it from users and ends up with tickets on github
echo "signedRequest error: {$output}" . PHP_EOL;
}
$this->transfered += strlen($output);
$this->requestCount++;
return $json;
}
}