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

Commit

Permalink
Add WebSocket kline function + miniTicker example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Eyrick authored Apr 10, 2018
2 parents 0536d70 + 0b7d96b commit 4382989
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
[![Latest Version](https://img.shields.io/github/release/jaggedsoft/php-binance-api.svg?style=flat-square)](https://github.com/jaggedsoft/php-binance-api/releases) [![GitHub last commit](https://img.shields.io/github/last-commit/jaggedsoft/php-binance-api.svg?style=flat-square)](#) [![HitCount](http://hits.dwyl.io/jaggedsoft/php-binance-api.svg)](http://hits.dwyl.io/jaggedsoft/php-binance-api) [![Packagist Downloads](https://img.shields.io/packagist/dt/jaggedsoft/php-binance-api.svg?style=flat-square)](https://packagist.org/packages/jaggedsoft/php-binance-api) [![Build Status](https://travis-ci.org/jaggedsoft/php-binance-api.svg?branch=master&style=flat-square)](https://travis-ci.org/jaggedsoft/php-binance-api) [![Coverage Status](https://coveralls.io/repos/github/jaggedsoft/php-binance-api/badge.svg?branch=master&style=flat-square)](https://coveralls.io/github/jaggedsoft/php-binance-api) [![CodeCov](https://codecov.io/gh/jaggedsoft/php-binance-api/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/github/jaggedsoft/php-binance-api/) [![Code consistency](https://squizlabs.github.io/PHP_CodeSniffer/analysis/jaggedsoft/php-binance-api/grade.svg?style=flat-square)](https://squizlabs.github.io/PHP_CodeSniffer/analysis/jaggedsoft/php-binance-api)
[![Latest Version](https://img.shields.io/github/release/jaggedsoft/php-binance-api.svg?style=flat-square)](https://github.com/jaggedsoft/php-binance-api/releases)
[![GitHub last commit](https://img.shields.io/github/last-commit/jaggedsoft/php-binance-api.svg?style=flat-square)](#)
[![HitCount](http://hits.dwyl.io/jaggedsoft/php-binance-api.svg)](http://hits.dwyl.io/jaggedsoft/php-binance-api)
[![Packagist Downloads](https://img.shields.io/packagist/dt/jaggedsoft/php-binance-api.svg?style=flat-square)](https://packagist.org/packages/jaggedsoft/php-binance-api)


[![Build Status](https://travis-ci.org/jaggedsoft/php-binance-api.svg?branch=master&style=flat-square)](https://travis-ci.org/jaggedsoft/php-binance-api)
[![Coverage Status](https://coveralls.io/repos/github/jaggedsoft/php-binance-api/badge.svg?branch=master&style=flat-square)](https://coveralls.io/github/jaggedsoft/php-binance-api)
[![CodeCov](https://codecov.io/gh/jaggedsoft/php-binance-api/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/github/jaggedsoft/php-binance-api/)
[![Code consistency](https://squizlabs.github.io/PHP_CodeSniffer/analysis/jaggedsoft/php-binance-api/grade.svg?style=flat-square)](https://squizlabs.github.io/PHP_CodeSniffer/analysis/jaggedsoft/php-binance-api)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/683459a5a71c4875956cf23078a0c39b)](https://www.codacy.com/app/dmzoneill/php-binance-api?utm_source=github.com&utm_medium=referral&utm_content=jaggedsoft/php-binance-api&utm_campaign=Badge_Grade)

# PHP Binance API
This project is designed to help you make your own projects that interact with the [Binance API](https://github.com/binance-exchange/binance-official-api-docs). You can stream candlestick chart data, market depth, or use other advanced features such as setting stop losses and iceberg orders. This project seeks to have complete API coverage including WebSockets.

> Special thank you to all contributors: **dmzoneill, dxjones, jokaorgua,** and others!! *This package needs help from the community.* Improvements contributed to this project are encouraged, and you will be given full credit for changes. All pull requests welcome.
## [Getting Started Documentation](https://github.com/jaggedsoft/php-binance-api/wiki/1.-Getting-Started)
#### Getting started
```php
require 'vendor/autoload.php';
$api = new Binance\API("<api key>","<secret>");
```

#### Get latest price of a symbol
```php
$ticker = $api->prices();
print_r($ticker); // List prices of all symbols
echo "Price of BNB: {$ticker['BNBBTC']} BTC";
```

### [Documentation](https://github.com/jaggedsoft/php-binance-api/wiki/1.-Getting-Started)
> The primary documentation can be found on the [wiki](https://github.com/jaggedsoft/php-binance-api/wiki). There are also numerous other formats available. if you would like the markdown format of the wiki, you can clone it using:
```
git clone https://github.com/jaggedsoft/php-binance-api.wiki.git
Expand Down
61 changes: 54 additions & 7 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ public function order( string $side, string $symbol, $quantity, $price, string $
}

/**
* candlesticks get the cancles for the given intervals
* candlesticks get the candles for the given intervals
* 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
*
* $candles = $api->candlesticks("BNBBTC", "5m");
Expand Down Expand Up @@ -1672,11 +1672,10 @@ public function chart( $symbols, string $interval = "30m", Callable $callback )
$this->chartQueue[ $symbol ] = [];
$this->chartQueue[ $symbol ][ $interval ] = [];
$this->info[ $symbol ][ $interval ][ 'firstOpen' ] = 0;
// $this->info[$symbol]['chartCallback'.$interval] = $callback;
$endpoint = strtolower( $symbol ) . '@kline_' . $interval;
$this->subscriptions[ $endpoint ] = true;
$connector( $this->stream . $endpoint )->then( function ( $ws ) use ($callback, $symbol, $loop, $endpoint, $interval ) {
$ws->on( 'message', function ( $data ) use ($ws, $loop, $callback, $endpoint ) {
$connector( $this->stream . $endpoint )->then( function ( $ws ) use ( $callback, $symbol, $loop, $endpoint, $interval ) {
$ws->on( 'message', function ( $data ) use ( $ws, $loop, $callback, $endpoint ) {
if( $this->subscriptions[ $endpoint ] === false ) {
//$this->subscriptions[$endpoint] = null;
$loop->stop();
Expand All @@ -1689,11 +1688,11 @@ public function chart( $symbols, string $interval = "30m", Callable $callback )
$this->chartHandler( $symbol, $interval, $json );
call_user_func( $callback, $this, $symbol, $this->charts[ $symbol ][ $interval ] );
} );
$ws->on( 'close', function ( $code = null, $reason = null ) use ($symbol, $loop, $interval ) {
$ws->on( 'close', function ( $code = null, $reason = null ) use ( $symbol, $loop, $interval ) {
echo "chart({$symbol},{$interval}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
$loop->stop();
} );
}, function ( $e ) use ($loop, $symbol, $interval ) {
}, function ( $e ) use ( $loop, $symbol, $interval ) {
echo "chart({$symbol},{$interval})) Could not connect: {$e->getMessage()}" . PHP_EOL;
$loop->stop();
} );
Expand All @@ -1702,11 +1701,59 @@ public function chart( $symbols, string $interval = "30m", Callable $callback )
$this->chartHandler( $symbol, $interval, $json );
}
$this->chartQueue[ $symbol ][ $interval ] = [];
// $this->info[$symbol]['chartCallback'.$interval]($this, $symbol, $this->charts[$symbol][$interval]);
call_user_func( $callback, $this, $symbol, $this->charts[ $symbol ][ $interval ] );
}
$loop->run();
}

/**
* kline Subscribes to @klines WebSocket endpoint for latest chart data only
*
* $api->kline(["BNBBTC"], "15m", function($api, $symbol, $chart) {
* echo "{$symbol} chart update\n";
* print_r($chart);
* });
*
* @param $symbols string required symbols
* @param $interval string time inteval
* @param $callback callable closure
* @return null
* @throws \Exception
*/
public function kline( $symbols, string $interval = "30m", 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 ) {
$endpoint = strtolower( $symbol ) . '@kline_' . $interval;
$this->subscriptions[ $endpoint ] = true;
$connector( $this->stream . $endpoint )->then( function ( $ws ) use ( $callback, $symbol, $loop, $endpoint, $interval ) {
$ws->on( 'message', function ( $data ) use ( $ws, $loop, $callback, $endpoint ) {
if( $this->subscriptions[ $endpoint ] === false ) {
$loop->stop();
return;
}
$json = json_decode( $data );
$chart = $json->k;
$symbol = $json->s;
$interval = $chart->i;
call_user_func( $callback, $this, $symbol, $this->chart );
} );
$ws->on( 'close', function ( $code = null, $reason = null ) use ( $symbol, $loop, $interval ) {
echo "kline({$symbol},{$interval}) WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
$loop->stop();
} );
}, function ( $e ) use ( $loop, $symbol, $interval ) {
echo "kline({$symbol},{$interval})) Could not connect: {$e->getMessage()}" . PHP_EOL;
$loop->stop();
} );
}
$loop->run();
}

/**
* terminate Terminates websocket endpoints. View endpoints first: print_r($api->subscriptions)
Expand Down

0 comments on commit 4382989

Please sign in to comment.