Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit 0901188

Browse files
author
Jon Eyrick
authored
Fixed userData function
1 parent adf7bea commit 0901188

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

php-binance-api.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,34 @@ public function keepAlive() {
731731
});
732732
$loop->run();
733733
}
734+
735+
// Issues userDataStream token and keepalive, subscribes to userData WebSocket
736+
public function userData(&$balance_callback, &$execution_callback = false) {
737+
$response = $this->apiRequest("v1/userDataStream", "POST");
738+
$listenKey = $this->options['listenKey'] = $response['listenKey'];
739+
$this->info['balanceCallback'] = $balance_callback;
740+
$this->info['executionCallback'] = $execution_callback;
741+
\Ratchet\Client\connect('wss://stream.binance.com:9443/ws/'.$listenKey)->then(function($ws) {
742+
$ws->on('message', function($data) use($ws) {
743+
$json = json_decode($data);
744+
$type = $json->e;
745+
if ( $type == "outboundAccountInfo") {
746+
$balances = $this->balanceHandler($json->B);
747+
$this->info['balanceCallback']($this, $balances);
748+
} elseif ( $type == "executionReport" ) {
749+
$report = $this->executionHandler($json);
750+
if ( $this->info['executionCallback'] ) {
751+
$this->info['executionCallback']($this, $report);
752+
}
753+
}
754+
});
755+
$ws->on('close', function($code = null, $reason = null) {
756+
echo "userData: WebSocket Connection closed! ({$code} - {$reason})".PHP_EOL;
757+
});
758+
}, function($e) {
759+
echo "userData: Could not connect: {$e->getMessage()}".PHP_EOL;
760+
});
761+
}
734762

735763
public function miniTicker($callback) {
736764
\Ratchet\Client\connect('wss://stream2.binance.com:9443/ws/!miniTicker@arr@1000ms')

0 commit comments

Comments
 (0)