php-binance-api.php
public function userData(&$balance_callback, &$execution_callback = false)
    {
        $response = $this->httpRequest("v1/userDataStream", "POST", []);
        $this->listenKey = $response['listenKey'];
        $this->info['balanceCallback'] = $balance_callback;
        $this->info['executionCallback'] = $execution_callback;

        $this->subscriptions['@userdata'] = true;

        // @codeCoverageIgnoreStart
        // phpunit can't cover async function
        \Ratchet\Client\connect($this->stream . $this->listenKey)->then(function ($ws) {
            $ws->on('message', function ($data) use ($ws) {
                if ($this->subscriptions['@userdata'] === false) {
                    //$this->subscriptions[$endpoint] = null;
                    $ws->close();
                    return; //return $ws->close();
                }
                $json = json_decode($data);
                $type = $json->e;
                if ($type === "outboundAccountInfo") {
                    $balances = $this->balanceHandler($json->B);
                    $this->info['balanceCallback']($this, $balances);
                } elseif ($type === "executionReport") {
                    $report = $this->executionHandler($json);
                    if ($this->info['executionCallback']) {
                        $this->info['executionCallback']($this, $report);
                    }
                }
            });
            $ws->on('close', function ($code = null, $reason = null) {
                // WPCS: XSS OK.
                echo "userData: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
            });
        }, function ($e) {
            // WPCS: XSS OK.
            echo "userData: Could not connect: {$e->getMessage()}" . PHP_EOL;
        });
        // @codeCoverageIgnoreEnd
    }
public function userData(&$balance_callback, &$execution_callback = false)
    {
        $response = $this->httpRequest("v1/userDataStream", "POST", []);
        $this->listenKey = $response['listenKey'];
        $this->info['balanceCallback'] = $balance_callback;
        $this->info['executionCallback'] = $execution_callback;

        $this->subscriptions['@userdata'] = true;

        // @codeCoverageIgnoreStart
        // phpunit can't cover async function
        \Ratchet\Client\connect($this->stream . $this->listenKey)->then(function ($ws) {
            $ws->on('message', function ($data) use ($ws) {
                if ($this->subscriptions['@userdata'] === false) {
                    //$this->subscriptions[$endpoint] = null;
                    $ws->close();
                    return; //return $ws->close();
                }
                $json = json_decode($data);
                $type = $json->e;
                if ($type === "outboundAccountInfo") {
                    $balances = $this->balanceHandler($json->B);
                    $this->info['balanceCallback']($this, $balances);
                } elseif ($type === "executionReport") {
                    $report = $this->executionHandler($json);
                    if ($this->info['executionCallback']) {
                        $this->info['executionCallback']($this, $report);
                    }
                }
            });
            $ws->on('close', function ($code = null, $reason = null) {
                // WPCS: XSS OK.
                echo "userData: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
            });
        }, function ($e) {
            // WPCS: XSS OK.
            echo "userData: Could not connect: {$e->getMessage()}" . PHP_EOL;
        });
        // @codeCoverageIgnoreEnd
    }
public function userData ( & $ balance_callback , & $ execution_callback = false ) { $ response = $ this -> httpRequest ( "v1/userDataStream" , "POST" , [ ] ) ; $ this -> listenKey = $ response [ 'listenKey' ] ; $ this -> info [ 'balanceCallback' ] = $ balance_callback ; $ this -> info [ 'executionCallback' ] = $ execution_callback ; $ this -> subscriptions [ '@userdata' ] = true ; // @codeCoverageIgnoreStart // phpunit can't cover async function \ Ratchet \ Client \ connect ( $ this -> stream . $ this -> listenKey ) -> then ( function ( $ ws ) { $ ws -> on ( 'message' , function ( $ data ) use ( $ ws ) { if ( $ this -> subscriptions [ '@userdata' ] === false ) { //$this->subscriptions[$endpoint] = null; $ ws -> close ( ) ; return ; //return $ws->close(); } $ json = json_decode ( $ data ) ; $ type = $ json -> e ; if ( $ type === "outboundAccountInfo" ) { $ balances = $ this -> balanceHandler ( $ json -> B ) ; $ this -> info [ 'balanceCallback' ] ( $ this , $ balances ) ; } elseif ( $ type === "executionReport" ) { $ report = $ this -> executionHandler ( $ json ) ; if ( $ this -> info [ 'executionCallback' ] ) { $ this -> info [ 'executionCallback' ] ( $ this , $ report ) ; } } } ) ; $ ws -> on ( 'close' , function ( $ code = null , $ reason = null ) { // WPCS: XSS OK. echo "userData: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL ; } ) ; } , function ( $ e ) { // WPCS: XSS OK. echo "userData: Could not connect: {$e->getMessage()}" . PHP_EOL ; } ) ; // @codeCoverageIgnoreEnd }
userData Issues userDataStream token and keepalive, subscribes to userData WebSocket

$balance_update = function($api, $balances) {
print_r($balances);
echo "Balance update".PHP_EOL;
};

$order_update = function($api, $report) {
echo "Order update".PHP_EOL;
print_r($report);
$price = $report['price'];
$quantity = $report['quantity'];
$symbol = $report['symbol'];
$side = $report['side'];
$orderType = $report['orderType'];
$orderId = $report['orderId'];
$orderStatus = $report['orderStatus'];
$executionType = $report['orderStatus'];
if( $executionType == "NEW" ) {
if( $executionType == "REJECTED" ) {
echo "Order Failed! Reason: {$report['rejectReason']}".PHP_EOL;
}
echo "{$symbol} {$side} {$orderType} ORDER #{$orderId} ({$orderStatus})".PHP_EOL;
echo "..price: {$price}, quantity: {$quantity}".PHP_EOL;
return;
}

//NEW, CANCELED, REPLACED, REJECTED, TRADE, EXPIRED
echo "{$symbol} {$side} {$executionType} {$orderType} ORDER #{$orderId}".PHP_EOL;
};
$api->userData($balance_update, $order_update);

@param $balance_callback callable function
@param bool $execution_callback callable function
@return null
@throws \Exception
userData Issues userDataStream token and keepalive subscribes to userData WebSocket