Skip to content

Commit

Permalink
add miniTicker function (thanks mrsantran!)
Browse files Browse the repository at this point in the history
```php
$api->miniTicker(function($api, $ticker) {
	print_r($ticker);
});
```
```
    [7] => Array
        (
            [symbol] => LTCUSDT
            [close] => 182.85000000
            [open] => 192.62000000
            [high] => 195.25000000
            [low] => 173.08000000
            [volume] => 238603.66451000
            [quoteVolume] => 43782422.11276660
            [eventTime] => 1520497914289
        )

    [8] => Array
        (
            [symbol] => ICXBTC
            [close] => 0.00029790
            [open] => 0.00030550
            [high] => 0.00031600
            [low] => 0.00026850
            [volume] => 8468620.53000000
            [quoteVolume] => 2493.60935828
            [eventTime] => 1520497915200
        )
```
  • Loading branch information
Jon Eyrick authored Mar 8, 2018
1 parent cb04647 commit d025f07
Showing 1 changed file with 21 additions and 37 deletions.
58 changes: 21 additions & 37 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,47 +639,31 @@ public function keepAlive() {
$loop->run();
}

// Issues userDataStream token and keepalive, subscribes to userData WebSocket
public function userData(&$balance_callback, &$execution_callback = false) {
$response = $this->apiRequest("v1/userDataStream", "POST");
$listenKey = $this->options['listenKey'] = $response['listenKey'];
$this->info['balanceCallback'] = $balance_callback;
$this->info['executionCallback'] = $execution_callback;
\Ratchet\Client\connect('wss://stream.binance.com:9443/ws/'.$listenKey)->then(function($ws) {
$ws->on('message', function($data) use($ws) {
$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) {
echo "userData: WebSocket Connection closed! ({$code} - {$reason})".PHP_EOL;
});
}, function($e) {
echo "userData: Could not connect: {$e->getMessage()}".PHP_EOL;
});
}

public function miniTicker($callback)
{
\Ratchet\Client\connect('wss://stream2.binance.com:9443/ws/!miniTicker@arr@3000ms')
public function miniTicker($callback) {
\Ratchet\Client\connect('wss://stream2.binance.com:9443/ws/!miniTicker@arr@1000ms')
->then(function($ws) use($callback) {
$ws->on('message', function($data) use($ws, $callback) {
$json = json_decode($data);
call_user_func($callback, $this, $json);
});
$ws->on('close', function($code = null, $reason = null) {
echo "ticker: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
$json = json_decode($data, true);
$markets = [];
foreach ( $json as $obj ) {
$markets[] = [
"symbol" => $obj['s'],
"close" => $obj['c'],
"open" => $obj['o'],
"high" => $obj['h'],
"low" => $obj['l'],
"volume" => $obj['v'],
"quoteVolume" => $obj['q'],
"eventTime" => $obj['E']
];
}
call_user_func($callback, $this, $symbols);
});
$ws->on('close', function($code = null, $reason = null) {
echo "miniticker: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
});
}, function($e) {
echo "ticker: Could not connect: {$e->getMessage()}" . PHP_EOL;
echo "miniticker: Could not connect: {$e->getMessage()}" . PHP_EOL;
});
}
}

0 comments on commit d025f07

Please sign in to comment.