A PHP class for the CEX.io Spot Trading API: https://trade.cex.io/docs/
If you find this library useful, consider signing up for CEX.io using my referral link:
It's free and helps support the continued development of this project.
This version has been updated to work with the new CEX.io Spot Trading API. The old API endpoints (https://cex.io/api) have been replaced with the new Spot Trading API (https://trade.cex.io/api/spot/).
- PHP 7.0 or higher
- cURL extension enabled
- OpenSSL extension (for HTTPS)
- Download the API source files
- Generate your API key and API secret on https://trade.cex.io/
- Include the CEX class in your project
-
Copy the environment template and add your API credentials:
cp .env.example .env
-
Edit
.envwith your API keys from https://trade.cex.io/ -
Run tests:
source .env && ./vendor/bin/phpunit tests/CEXTest.php
The .env file is ignored by git to keep your credentials safe.
include_once("cex.class.php");
// API credentials (optional for public methods)
$api_key = 'your_api_key';
$api_secret = 'your_api_secret';
$api_cert = 'cacert.pem'; // Optional: path to CA certificate bundle
// Create CEX object
$CEX = new CEX($api_key, $api_secret, $api_cert);
// Public method example
$ticker = $CEX->ticker('BTC/USD');
var_dump($ticker);
// Private method example (requires API credentials)
$balance = $CEX->balance();
var_dump($balance);These methods don't require API credentials.
$server_time = $CEX->server_time();Returns information about all available trading pairs.
$pairs = $CEX->pairs_info();Returns an array of available symbol pairs (backward compatible).
$symbols = $CEX->symbols();
// Returns: ['BTC/USD', 'ETH/USD', ...]Returns information about all available currencies.
$currencies = $CEX->currencies_info();Get market data for a trading pair.
$ticker = $CEX->ticker('BTC/USD');
// Returns: ['last', 'high', 'low', 'volume', 'bid', 'ask', 'pair', 'raw']Get ticker data for multiple pairs at once.
$tickers = $CEX->tickers(['BTC/USD', 'ETH/USD']);Get the last trade price for a trading pair.
$last_price = $CEX->last_price('BTC/USD');
// Returns: ['lprice', 'pair']Get the order book with bids and asks.
// Full order book
$order_book = $CEX->order_book('BTC/USD');
// Limited depth
$order_book = $CEX->order_book('BTC/USD', 10);Get recent trades for a trading pair.
$trades = $CEX->trade_history('BTC/USD');
// With since parameter
$trades = $CEX->trade_history('BTC/USD', $since_trade_id);Get candlestick/kline data for charting.
$from = time() - (24 * 3600);
$to = time();
$candles = $CEX->candles('BTC/USD', '1h', $from, $to);
// Resolutions: 1m, 5m, 15m, 30m, 1h, 4h, 1d, 1wThese methods require valid API credentials.
Get account balances for all currencies.
$balance = $CEX->balance();Get the current trading fee for a pair.
$fee = $CEX->my_fee('BTC/USD');Get orders based on filter criteria.
// All open orders for a pair
$orders = $CEX->my_orders('BTC/USD', 'open', 100);
// Status options: 'open', 'closed', 'canceled', 'all'Get list of open orders for a trading pair.
$open_orders = $CEX->open_orders('BTC/USD');Create a new limit order.
$order = $CEX->place_order('BTC/USD', 'buy', 0.001, 50000);
// Parameters: pair, type (buy/sell), amount, priceCreate a market order.
$order = $CEX->place_market_order('BTC/USD', 'buy', 0.001);
// Parameters: pair, type (buy/sell), amountCancel an existing order.
$result = $CEX->cancel_order($order_id);
// Returns: true on success, false on failureCancel all open orders.
// Cancel all orders for a specific pair
$result = $CEX->cancel_all_orders('BTC/USD');
// Cancel all orders for all pairs
$result = $CEX->cancel_all_orders();Get ledger/transaction history.
$history = $CEX->transaction_history('BTC', 100);
// Parameters: currency (optional), limitGet deposit/withdrawal history.
$history = $CEX->funding_history('BTC', 100);
// Parameters: currency (optional), limitGet deposit address for a currency.
$address = $CEX->deposit_address('BTC');Transfer funds between CEX.io accounts.
$result = $CEX->internal_transfer('BTC', 0.01, $to_account_id);The library accepts both old-style (BTC/USD) and new-style (BTC-USD) symbol formats. They are automatically converted as needed.
All methods return an array. Check for errors by looking for the error key:
$result = $CEX->ticker('BTC/USD');
if (isset($result['error'])) {
echo "Error: " . $result['error'];
} else {
echo "Last price: " . $result['last'];
}Old:
$CEX = new CEX($username, $key, $secret, 'https://cex.io/api', 'cacert.pem');New:
$CEX = new CEX($key, $secret, 'cacert.pem');
// Note: username is no longer required| Old Method | New Method | Notes |
|---|---|---|
symbols() |
symbols() |
Now fetches dynamically from API |
ticker($pair) |
ticker($pair) |
Response format changed |
last_price($pair) |
last_price($pair) |
Response format changed |
order_book($pair, $depth) |
order_book($pair, $depth) |
Response format similar |
trade_history($pair, $since) |
trade_history($pair, $since) |
Response format changed |
price_stats(...) |
candles(...) |
Use candles() instead |
convert($pair, $value) |
Removed | Use ticker() and calculate manually |
balance() |
balance() |
Response format changed |
open_orders($pair) |
open_orders($pair) |
Works the same |
place_order(...) |
place_order(...) |
Parameter names changed |
cancel_order($id) |
cancel_order($id) |
Works the same |
hashrate() |
Removed | GHash.io discontinued |
workers() |
Removed | GHash.io discontinued |
server_time()- Get server timestamppairs_info()- Get detailed pair informationcurrencies_info()- Get currency informationtickers($pairs)- Get multiple tickers at oncecandles($pair, $resolution, $from, $to)- Get OHLCV datamy_fee($pair)- Get trading feemy_orders($pair, $status, $limit)- Get orders with filtersplace_market_order($pair, $type, $amount)- Place market ordercancel_all_orders($pair)- Cancel all orderstransaction_history($currency, $limit)- Get transaction historyfunding_history($currency, $limit)- Get funding historydeposit_address($currency)- Get deposit addressinternal_transfer($currency, $amount, $to)- Internal transfer
For complete API documentation, visit: https://trade.cex.io/docs/
MIT License - see LICENSE file for details.
Roy Boverhof - https://bsky.app/profile/boverhof.bsky.social
Contributions are welcome! Please submit pull requests to the GitHub repository.