php-binance-api.php
public function trades($symbols, callable $callback)
    {
        if (!is_array($symbols)) {
            $symbols = [
                $symbols,
            ];
        }

        $loop = \React\EventLoop\Factory::create();
        $react = new \React\Socket\Connector($loop);
        $connector = new \Ratchet\Client\Connector($loop, $react);
        foreach ($symbols as $symbol) {
            if (!isset($this->info[$symbol])) {
                $this->info[$symbol] = [];
            }

            // $this->info[$symbol]['tradesCallback'] = $callback;

            $endpoint = strtolower($symbol) . '@trades';
            $this->subscriptions[$endpoint] = true;

            $connector($this->stream . strtolower($symbol) . '@aggTrade')->then(function ($ws) use ($callback, $symbol, $loop, $endpoint) {
                $ws->on('message', function ($data) use ($ws, $callback, $loop, $endpoint) {
                    if ($this->subscriptions[$endpoint] === false) {
                        //$this->subscriptions[$endpoint] = null;
                        $loop->stop();
                        return; //return $ws->close();
                    }
                    $json = json_decode($data, true);
                    $symbol = $json['s'];
                    $price = $json['p'];
                    $quantity = $json['q'];
                    $timestamp = $json['T'];
                    $maker = $json['m'] ? 'true' : 'false';
                    $trades = [
                        "price" => $price,
                        "quantity" => $quantity,
                        "timestamp" => $timestamp,
                        "maker" => $maker,
                    ];
                    // $this->info[$symbol]['tradesCallback']($this, $symbol, $trades);
                    call_user_func($callback, $this, $symbol, $trades);
                });
                $ws->on('close', function ($code = null, $reason = null) use ($symbol, $loop) {
                    // WPCS: XSS OK.
                    echo "trades({$symbol}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
                    $loop->stop();
                });
            }, function ($e) use ($loop, $symbol) {
                // WPCS: XSS OK.
                echo "trades({$symbol}) Could not connect: {$e->getMessage()}" . PHP_EOL;
                $loop->stop();
            });
        }
        $loop->run();
    }
public function trades($symbols, callable $callback)
    {
        if (!is_array($symbols)) {
            $symbols = [
                $symbols,
            ];
        }

        $loop = \React\EventLoop\Factory::create();
        $react = new \React\Socket\Connector($loop);
        $connector = new \Ratchet\Client\Connector($loop, $react);
        foreach ($symbols as $symbol) {
            if (!isset($this->info[$symbol])) {
                $this->info[$symbol] = [];
            }

            // $this->info[$symbol]['tradesCallback'] = $callback;

            $endpoint = strtolower($symbol) . '@trades';
            $this->subscriptions[$endpoint] = true;

            $connector($this->stream . strtolower($symbol) . '@aggTrade')->then(function ($ws) use ($callback, $symbol, $loop, $endpoint) {
                $ws->on('message', function ($data) use ($ws, $callback, $loop, $endpoint) {
                    if ($this->subscriptions[$endpoint] === false) {
                        //$this->subscriptions[$endpoint] = null;
                        $loop->stop();
                        return; //return $ws->close();
                    }
                    $json = json_decode($data, true);
                    $symbol = $json['s'];
                    $price = $json['p'];
                    $quantity = $json['q'];
                    $timestamp = $json['T'];
                    $maker = $json['m'] ? 'true' : 'false';
                    $trades = [
                        "price" => $price,
                        "quantity" => $quantity,
                        "timestamp" => $timestamp,
                        "maker" => $maker,
                    ];
                    // $this->info[$symbol]['tradesCallback']($this, $symbol, $trades);
                    call_user_func($callback, $this, $symbol, $trades);
                });
                $ws->on('close', function ($code = null, $reason = null) use ($symbol, $loop) {
                    // WPCS: XSS OK.
                    echo "trades({$symbol}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
                    $loop->stop();
                });
            }, function ($e) use ($loop, $symbol) {
                // WPCS: XSS OK.
                echo "trades({$symbol}) Could not connect: {$e->getMessage()}" . PHP_EOL;
                $loop->stop();
            });
        }
        $loop->run();
    }
public function trades ( $ symbols , callable $ callback ) { if ( ! is_array ( $ symbols ) ) { $ symbols = [ $ symbols , ] ; } $ loop = \ React \ EventLoop \ Factory :: create ( ) ; $ react = new \ React \ Socket \ Connector ( $ loop ) ; $ connector = new \ Ratchet \ Client \ Connector ( $ loop , $ react ) ; foreach ( $ symbols as $ symbol ) { if ( ! isset ( $ this -> info [ $ symbol ] ) ) { $ this -> info [ $ symbol ] = [ ] ; } // $this->info[$symbol]['tradesCallback'] = $callback; $ endpoint = strtolower ( $ symbol ) . '@trades' ; $ this -> subscriptions [ $ endpoint ] = true ; $ connector ( $ this -> stream . strtolower ( $ symbol ) . '@aggTrade' ) -> then ( function ( $ ws ) use ( $ callback , $ symbol , $ loop , $ endpoint ) { $ ws -> on ( 'message' , function ( $ data ) use ( $ ws , $ callback , $ loop , $ endpoint ) { if ( $ this -> subscriptions [ $ endpoint ] === false ) { //$this->subscriptions[$endpoint] = null; $ loop -> stop ( ) ; return ; //return $ws->close(); } $ json = json_decode ( $ data , true ) ; $ symbol = $ json [ 's' ] ; $ price = $ json [ 'p' ] ; $ quantity = $ json [ 'q' ] ; $ timestamp = $ json [ 'T' ] ; $ maker = $ json [ 'm' ] ? 'true' : 'false' ; $ trades = [ "price" => $ price , "quantity" => $ quantity , "timestamp" => $ timestamp , "maker" => $ maker , ] ; // $this->info[$symbol]['tradesCallback']($this, $symbol, $trades); call_user_func ( $ callback , $ this , $ symbol , $ trades ) ; } ) ; $ ws -> on ( 'close' , function ( $ code = null , $ reason = null ) use ( $ symbol , $ loop ) { // WPCS: XSS OK. echo "trades({$symbol}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL ; $ loop -> stop ( ) ; } ) ; } , function ( $ e ) use ( $ loop , $ symbol ) { // WPCS: XSS OK. echo "trades({$symbol}) Could not connect: {$e->getMessage()}" . PHP_EOL ; $ loop -> stop ( ) ; } ) ; } $ loop -> run ( ) ; }
trades Trades WebSocket Endpoint

$api->trades(["BNBBTC"], function($api, $symbol, $trades) {
echo "{$symbol} trades update".PHP_EOL;
print_r($trades);
});

@param $symbols
@param $callback callable closure
@return null
trades Trades WebSocket Endpoint