Skip to content

Commit 25fea7e

Browse files
authored
added bookTicker wss by bright-light-in-the-night
2 parents cad1b71 + 7702465 commit 25fea7e

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

php-binance-api.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,6 +2188,53 @@ public function miniTicker(callable $callback)
21882188
// @codeCoverageIgnoreEnd
21892189
}
21902190

2191+
/**
2192+
* bookTicker Get bookTicker for all symbols
2193+
*
2194+
* $api->bookTicker(function($api, $ticker) {
2195+
* print_r($ticker);
2196+
* });
2197+
*
2198+
* @param $callback callable function closer that takes 2 arguments, $api and $ticker data
2199+
* @return null
2200+
*/
2201+
public function bookTicker(callable $callback)
2202+
{
2203+
$endpoint = '!bookticker';
2204+
$this->subscriptions[$endpoint] = true;
2205+
2206+
// @codeCoverageIgnoreStart
2207+
// phpunit can't cover async function
2208+
\Ratchet\Client\connect($this->stream . '!bookTicker')->then(function ($ws) use ($callback, $endpoint) {
2209+
$ws->on('message', function ($data) use ($ws, $callback, $endpoint) {
2210+
if ($this->subscriptions[$endpoint] === false) {
2211+
//$this->subscriptions[$endpoint] = null;
2212+
$ws->close();
2213+
return; //return $ws->close();
2214+
}
2215+
$json = json_decode($data, true);
2216+
2217+
$markets = [
2218+
"updateId" => $json['u'],
2219+
"symbol" => $json['s'],
2220+
"bid_price" => $json['b'],
2221+
"bid_qty" => $json['B'],
2222+
"ask_price" => $json['a'],
2223+
"ask_qty" => $json['A'],
2224+
];
2225+
call_user_func($callback, $this, $markets);
2226+
});
2227+
$ws->on('close', function ($code = null, $reason = null) {
2228+
// WPCS: XSS OK.
2229+
echo "miniticker: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
2230+
});
2231+
}, function ($e) {
2232+
// WPCS: XSS OK.
2233+
echo "miniticker: Could not connect: {$e->getMessage()}" . PHP_EOL;
2234+
});
2235+
// @codeCoverageIgnoreEnd
2236+
}
2237+
21912238
/**
21922239
* Due to ongoing issues with out of date wamp CA bundles
21932240
* This function downloads ca bundle for curl website

0 commit comments

Comments
 (0)