Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,28 @@ class API
{
protected $base = 'https://api.binance.com/api/'; // /< REST endpoint for the currency exchange
protected $baseTestnet = 'https://testnet.binance.vision/api/'; // /< Testnet REST endpoint for the currency exchange
protected $baseTestnetBackup = 'https://testnet.binance.vision/api/';
protected $baseDemo = 'https://demo-api.binance.com/api/'; // /< Demo REST endpoint for the currency exchange
protected $wapi = 'https://api.binance.com/wapi/'; // /< REST endpoint for the withdrawals
protected $sapi = 'https://api.binance.com/sapi/'; // /< REST endpoint for the supporting network API
protected $fapi = 'https://fapi.binance.com/fapi/'; // /< REST endpoint for the futures API
protected $fapiData = 'https://fapi.binance.com/futures/data/'; // /< REST endpoint for the futures API
protected $fapiTestnet = 'https://testnet.binancefuture.com/fapi/'; // /< Testnet REST endpoint for the futures API
protected $fapiTestnetBackup = 'https://testnet.binancefuture.com/fapi/';
protected $fapiDemo = 'https://demo-fapi.binance.com/fapi/'; // /< Demo REST endpoint for the futures API
protected $dapi = 'https://dapi.binance.com/dapi/'; // /< REST endpoint for the delivery API
protected $dapiData = 'https://dapi.binance.com/futures/data/'; // /< REST endpoint for the delivery API
protected $dapiTestnet = 'https://testnet.binancefuture.com/dapi/'; // /< Testnet REST endpoint for the delivery API
protected $dapiTestnetBackup = 'https://testnet.binancefuture.com/dapi/';
protected $dapiDemo = 'https://demo-dapi.binance.com/dapi/'; // /< Demo REST endpoint for the delivery API
protected $papi = 'https://papi.binance.com/papi/'; // /< REST endpoint for the options API
protected $bapi = 'https://www.binance.com/bapi/'; // /< REST endpoint for the internal Binance API
protected $stream = 'wss://stream.binance.com:9443/ws/'; // /< Endpoint for establishing websocket connections
protected $streamTestnet = 'wss://testnet.binance.vision/ws/'; // /< Testnet endpoint for establishing websocket connections
protected $api_key; // /< API key that you created in the binance website member area
protected $api_secret; // /< API secret that was given to you when you created the api key
protected $useTestnet = false; // /< Enable/disable testnet (https://testnet.binance.vision/)
protected $useTestnet = false; // /< Enable/disable testnet
protected $useDemo = false; // /< Use demo endpoints for testnet
protected $depthCache = []; // /< Websockets depth cache
protected $depthQueue = []; // /< Websockets depth queue
protected $chartQueue = []; // /< Websockets chart queue
Expand Down Expand Up @@ -84,6 +91,7 @@ class API
* 1 argument - file to load config from
* 2 arguments - api key and api secret
* 3 arguments - api key, api secret and use testnet flag
* 4 arguments - api key, api secret, use testnet flag and use demo for testnet flag
*
* @return null
*/
Expand All @@ -110,11 +118,47 @@ public function __construct()
$this->api_secret = $param[1];
$this->useTestnet = (bool)$param[2];
break;
case 4:
$useDemo = (bool)$param[3];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yzh-pelle what if the user provides 3 args, (apiKey/secret/demo)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now to enable demo by default we would need to provide also testnet which is a bit weird, maybe we can replace testnet here with demo since testnet is deprecated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Third arg is for testnet. It is not python, args must be queued. We might add more examples for constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now to enable demo by default we would need to provide also testnet which is a bit weird, maybe we can replace testnet here with demo since testnet is deprecated

Yeah I was thinking about it. We may leave constructor as is, set default testnet url to demo and add method like useOldUrlForTestnet

$this->api_key = $param[0];
$this->api_secret = $param[1];
$this->useTestnet = (bool)$param[2];
$this->enableDemoTrading($useDemo);
break;
default:
echo 'Please see valid constructors here: https://github.com/jaggedsoft/php-binance-api/blob/master/examples/constructor.php';
}
}

/**
* enableDemoTrading - Enable or disable demo trading endpoints for testnet
*
* @param bool $enable true to enable demo trading endpoints, false to disable
*/
public function enableDemoTrading(?bool $enable = true)
{
if ($enable) {
$this->setDemoEndpoints();
} else {
$this->setTestnetEndpoints();
}
$this->useDemo = $enable;
}

protected function setDemoEndpoints()
{
$this->baseTestnet = $this->baseDemo;
$this->fapiTestnet = $this->fapiDemo;
$this->dapiTestnet = $this->dapiDemo;
}

protected function setTestnetEndpoints()
{
$this->baseTestnet = $this->baseTestnetBackup;
$this->fapiTestnet = $this->fapiTestnetBackup;
$this->dapiTestnet = $this->dapiTestnetBackup;
}

/**
* magic get for protected and protected members
*
Expand Down Expand Up @@ -164,6 +208,7 @@ protected function setupApiConfigFromFile(?string $file = null)
$this->api_key = isset($contents['api-key']) ? $contents['api-key'] : "";
$this->api_secret = isset($contents['api-secret']) ? $contents['api-secret'] : "";
$this->useTestnet = isset($contents['use-testnet']) ? (bool)$contents['use-testnet'] : false;
$this->useDemo = isset($contents['use-demo']) ? (bool)$contents['use-demo'] : false;
}

/**
Expand Down
Loading