<?php
$debug = false;
date_default_timezone_set ('UTC');
include 'ccxt/ccxt.php'; // where you put ccxt.php in relation to this program
include_once __DIR__ . '/vendor/autoload.php';
//get the API key stuffs, put them in your environment variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../', 'file.file');
$dotenv->load();
// ***Example exchanges, Create your own exchange Variables here**************************
$binanceus_1_key = $_ENV['bincanceus_1_key'];
$binanceus_1_secret = $_ENV['binanceus_1_secret'];
$kraken_1_key = $_ENV['kraken_1_key'];
$kraken_1_secret = $_ENV['kraken_1_secret'];
$bittrex_1_key = $_ENV['bittrex_1_key'];
$bittrex_1_secret = $_ENV['bittrex_1_secret'];
$coinbaseadvanced_1_key = $_ENV['coinbaseadvanced_1_key'];
$coinbaseadvanced_1_secret = $_ENV['coinbaseadvanced_1_secret'];
// *****************************************************************************************************
function my_echo($text) {
global $debug; // use global variable $debug inside the function
// if ($debug == true) { echo "$text" . "<br>"; } //looks better in browser
if ($debug == true) { echo "$text" . " : "; } //looks better in b4j logs
}
function Get($value, $default_value) { //$_GET if exsist, else return a default value
if (isset($_GET[$value])) {
$value = $_GET[$value];
return $value;
} else {
return $default_value;
}
}
$bot = Get("bot", "no bot"); // $_GET bot or default to 'bot'
$command = Get("command", "no command");
//$scoin = 'USD';
my_echo ('$bot = ' . $bot);
my_echo ('$command = ' . $command);
switch ($bot) {
case 'binanceus_1': // "new" creates a new object from a class
$exchange = new ccxt\binanceus (array (
'apiKey' => $binanceus_1_key,
'secret' => $binanceus_1_secret,
'enableRateLimit' => true,
));
break;
case 'kraken_1':
$exchange = new ccxt\kraken (array (
'apiKey' => $kraken_1_key,
'secret' => $kraken_1_secret,
'enableRateLimit' => true,
));
break;
case 'bittrex_1':
$exchange = new ccxt\bittrex (array (
'apiKey' => $bittrex_1_key,
'secret' => $bittrex_1_secret,
'enableRateLimit' => true,
));
break;
case 'coinbase_adv_1': // not supported by ccxt yet, as of 29-Jan-2023
$exchange = new ccxt\coinbaseadvanced (array (
//'password' => '',
'apiKey' => $coinbaseadvanced_1_key,
'secret' => $coinbaseadvanced_1_secret,
'enableRateLimit' => true,
));
break;
default: // no bot selected
}
try {
$res = array('API_call.php', 'found', 'no', 'JSON');
if ($bot == "no bot") $res = "Choose a bot First";
//---------------------------------------------------------- Public Calls
switch ($command) {
case 'Test_PHP':
$res = "API_call.php successful!!! ccxt requires PHP 7.0+, your PHP version: " . phpversion();
break;
case 'Test_CCXT':
$res = "CCXT v." . \ccxt\Exchange::VERSION;
break;
case 'exchanges':
$res = \ccxt\Exchange::$exchanges; //Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ).
break;
case 'exchange_info':
if ($bot != 'bot') $res = $exchange->describe(array('has'));
//$res = $exchange->describe(array('id'));
break;
case 'fetch_markets':
$res = $exchange->fetch_markets(array());
break;
case 'fetch_ticker':
$symbol = Get("symbol", null);
$res = $exchange->fetch_ticker ($symbol, array());
break;
case 'fetch_tickers':
//$symbol = Get('symbol', null);
$res = $exchange->fetch_tickers ();
break;
case 'fetch_l2_order_book':
$symbol = Get("symbol", null);
$limit = Get("limit", null);
$res = $exchange->fetch_l2_order_book($symbol, $limit, array());
//$res = $exchange->fetch_order_book($symbol, 30, array()); // can give more data than l2 orderbook
break;
case 'fetch_ohlcv':
$symbol = Get("symbol", null);
$timeframe = Get("timeframe", null);
$since = Get("since", null); //optional
$limit = Get("limit", null); //optional
$res = $exchange->fetch_ohlcv($symbol, $timeframe, $since, $limit, array ());
break;
case 'build_ohlcvc': //Doesn't work
$symbol = Get("symbol", null);
$timeframe = Get("timeframe", null);
$since = Get("since", null); //optional
$limit = Get("limit", null); //optional
$res = $exchange->fetch_ohlcvc($symbol, $timeframe, $since, $limit, array());
break;
case 'fetch_trades':
$symbol = Get("symbol", null);
$since = Get("since", null); //optional
$limit = Get("limit", null); //optional
$res = $exchange->fetch_trades($symbol, $since, $limit, array());
break;
//---------------------------------------------------------- Private Calls
case 'fetch_balance':
$res = $exchange->fetch_balance(array());
break;
case 'fetch_open_orders': //single coinpair
$symbol = Get("symbol", null);
$since = Get("since", null); //optional
$limit = Get("limit", null); //optional
$res = $exchange->fetch_open_orders($symbol, $since, $limit, array());
break;
case 'fetch_all_open_orders': //most exchanges allow all coinpairs at once?
$symbols = $exchange->symbols; //$symbols = array('ADA/BTC', 'MANA/BTC');
$since = Get("since", null); //optional
$limit = Get("limit", null); //optional
$res = $exchange->fetch_open_orders($symbols, $since, $limit, array());
//fetch_open_orders($symbol = null, $since = null, $limit = null, $params = array ()
break;
case 'fetch_my_trades':
$symbol = Get("symbol", null);
$since = Get("since", null); //optional
$limit = Get("limit", null); //optional
$res = $exchange->fetch_my_trades($symbol, $since, $limit, array());
break;
case 'fetch_all_my_trades': //All Coins History doesn't work with many exchanges
$symbols = $exchange->symbols;
$since = Get("since", null); //optional
$limit = Get("limit", null); //optional
$res = $exchange->fetch_my_trades($symbols, $since, $limit, array());
break;
case 'create_limit_buy_order':
$symbol = Get("symbol", null);
$amount = Get("amount", null);
$price = Get("price", null);
$res = $exchange->create_limit_buy_order($symbol, $amount, $price, array());
break;
case 'create_limit_sell_order':
$symbol = Get("symbol", null);
$amount = Get("amount", null);
$price = Get("price", null);
$res = $exchange->create_limit_sell_order($symbol, $amount, $price, array());
break;
case 'edit_limit_order':
$id = Get("id", null);
$symbol = Get("symbol", null);
$side = Get("side", null);
$amount = Get("amount", null);
$price = Get("price", null);
$res = $exchange->edit_limit_order($id, $symbol, $side, $amount, $price, array());
break;
case 'create_market_buy_order':
$symbol = Get("symbol", null);
$amount = Get("amount", null);
$res = $exchange->create_market_buy_order($symbol, $amount, array());
break;
case 'create_market_sell_order':
$symbol = Get("symbol", null);
$amount = Get("amount", null);
$res = $exchange->create_market_sell_order($symbol, $amount, array());
break;
case 'cancel_order':
$id = Get("id", null);
$res = $exchange->cancel_order($id, null, array());
//$symbol = Get("symbol", null);
//$res = $exchange->cancel_order($id, $symbol, array());
break;
case 'cancel_orders':
$ids = Get("ids", null);
$res = $exchange->cancelOrders($ids, array());
break;
case 'cancel_all_orders':
$res = $exchange->cancel_all_orders(null, array());
break;
default:
echo 'Api.call.php $command: "' . $command . '" not found. <br>';
} //end switch
// $json_res = json_encode($res, JSON_UNESCAPED_SLASHES). "\n";
$json_res = json_encode($res, JSON_UNESCAPED_SLASHES); // get rid of the \/'s
echo ($json_res);
// handle error
} catch (\ccxt\NetworkError $e) {
echo '(Network Error) ' . $e->getMessage () . "\n";
// echo $e->getMessage () . "\n";
} catch (\ccxt\ExchangeError $e) {
echo '(Exchange Error) ' . $e->getMessage () . "\n";
// echo $e->getMessage () . "\n";
} catch (Exception $e) {
echo '(Error) ' . $e->getMessage () . "\n";
// echo $e->getMessage () . "\n";
}
?>