infinex-exchange / infinex-php
官方Infinex API的PHP封装
1.1.0
2023-03-05 12:44 UTC
Requires
- evenement/evenement: ^3.0
- ratchet/pawl: ^0.4.1
- react/async: ^4.0
- react/http: ^1.8
- react/promise: ^3@dev
README
官方Infinex API的PHP封装。
安装
composer require infinex-exchange/infinex-php
使用
查看examples/文件夹以获取更多使用示例。
API
通过HTTP的阻塞API
<?php require __DIR__ . '/vendor/autoload.php'; try { $infinex = new Infinex\API( new Infinex\Transport\HTTP('https://api.infinex.cc') ); var_dump( $infinex -> wallet -> getAssets() ); $infinex -> login('api_key_here'); var_dump( $infinex -> wallet -> getBalance('USDT') ); var_dump( $infinex -> spot -> getOrderBook('BPX/USDT') ); } catch(Infinex\Exceptions\ConnException $e) { echo "Connection error: " . $e->getMessage(); } catch(Infinex\Exceptions\InfinexException $e) { echo "Error from exchange: " . $e->getMessage(); } ?>
通过HTTP的非阻塞异步API
<?php require __DIR__ . '/vendor/autoload.php'; $infinex = new Infinex\API( new Infinex\Transport\HTTP('https://api.infinex.cc'), true ); $infinex -> login('api_key_here'); $infinex -> wallet -> getBalance('BTC') -> then( function($response) { var_dump($response); }, function($e) { echo get_class($e).': '.$e->getMessage()."\n"; } ); ?>
通过WebSockets的阻塞API
<?php require __DIR__ . '/vendor/autoload.php'; use React\EventLoop\Loop; $loop = Loop::get(); try { $infinex = new Infinex\API( new Infinex\Transport\WebSocket($loop, 'wss://mux.infinex.cc') ); var_dump( $infinex -> wallet -> getAssets() ); $infinex -> login('api_key_here'); var_dump( $infinex -> wallet -> getBalance('USDT') ); var_dump( $infinex -> spot -> getOrderBook('BPX/USDT') ); } catch(Infinex\Exceptions\ConnException $e) { echo "Connection error: " . $e->getMessage(); } catch(Infinex\Exceptions\InfinexException $e) { echo "Error from exchange: " . $e->getMessage(); } $loop -> run(); ?>
通过WebSockets的非阻塞异步API
<?php require __DIR__ . '/vendor/autoload.php'; use React\EventLoop\Loop; $loop = Loop::get(); $infinex = new Infinex\API( new Infinex\Transport\WebSocket($loop, 'wss://mux.infinex.cc'), true ); $infinex -> login('api_key_here'); $infinex -> wallet -> getBalance('BTC') -> then( function($response) { var_dump($response); }, function($e) { echo get_class($e).': '.$e->getMessage()."\n"; } ); $loop -> run(); ?>
流
要使用Infinex流,您需要创建一个ReactPHP事件循环,初始化StreamsClient
对象并连接到交易所服务器
use React\EventLoop\Loop; $loop = Loop::get(); $infinex = new Infinex\StreamsClient($loop, 'wss://stream.infinex.cc'); $infinex -> open() -> then( function() { echo "Connection successfull\n"; }, function($e) { echo 'Connection failed: '.$e -> getMessage()."\n"; } );
如果连接中断,StreamsClient
对象将自动重新连接,重新登录并恢复所有订阅。然而,某些事件,例如订单簿更新,可能会在我们未连接时发生。因此,我们可以捕捉并响应连接丢失和恢复事件。
$infinex -> on('open', function() { echo "Connected to Infinex streams server\n"; }); $infinex -> on('close', function() { echo "Disconnected from Infinex streams server\n"; });
要订阅一个或多个流,我们可以使用sub
函数。第一个参数是流的名称或流名称数组,第二个参数是每次流接收到事件时将被调用的回调函数。
$infinex -> sub( 'BPX/USDT@ticker', function($event) { echo 'Alert! Market price changed to '.$event -> price."\n"; } ) -> then( function() { echo "Subscribed!\n"; }, function($e) { echo "Failed to subscribe! '.$e->getMessage()."\n"; } );
要取消订阅一个或多个流,请使用unsub
函数
$infinex -> unsub('BPX/USDT@ticker') -> then( function() { echo "Unsubscribed!\n"; }, function($e) { echo "Failed to unsubscribe! '.$e->getMessage()."\n"; } );
要使用私有流,首先使用您的API密钥登录交易所
$infinex -> login('api_key_here') -> then( function() { echo "Logged in\n"; }, function($e) { echo "Login error! ".$e -> getMessage()."\n"; } );