I am trying to call binance api class in construct method in my controller so that I can access that api class instance throught entire controller. Problem lies that I need to pass $key and $secret variables to binance class in order to get that object, but I can not do that in construct method. I tried making config.ini file and calling it with parse_ini_file but that returned error that I can not use that function inside class. Here is my code. Any help is appreciated or if someone has other idea on how to make this work.
controller
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
require 'vendor/autoload.php';
class BinanceController
{
private $api;
private $key = 'some long string 1';
private $secret = 'some long string 2';
$api = new Binance\API($key,$secret); // SOMEHOW I NEED LIKE THIS!!!
public function __construct()
{
$this->api = new Binance\API($this->key,$this->secret);
}
public function getAllBinancePairs()
{
$exchangeInfo = $this->api->exchangeInfo(); // HERE IS SOURCE OF ERROR!!!
$results = [];
foreach($exchangeInfo['symbols'] as $info) {
$results[] = $info['symbol'];
}
json_response($results);
}
}
index.php
<?php
require 'vendor/autoload.php';
$router = new AltoRouter();
$router->map( 'GET', '/fbinance', function() {
require __DIR__ . '/fbinance.php';
});
$router->map('GET','/get-all-binance-pairs', array('c' => 'BinanceController', 'a' => 'getAllBinancePairs'));
$match = $router->match();
if(!isset($match['target']['c']))
{
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Server Error', true, 404);
}
switch($match['target']['c']) {
case "BinanceController":
include 'controllers/BinanceController.php';
if( $match && is_callable("BinanceController::" . $match['target']['a']) ) {
call_user_func_array("BinanceController::" . $match['target']['a'], $match['params'] );
} else {
// no route was matched
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Server Error', true, 404);
exit;
}
break;
default:
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Server Error', true, 404);
break;
}
If the arguments to the constructor for that API are not public, you can only instantiate it within the constructor of BinanceController
like:
class BinanceController
{
private $api;
private $key = 'some long string 1';
private $secret = 'some long string 2';
public function __construct()
{
$this->api = new Binance\API($this->key,$this->secret);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.