Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
2pd committed May 19, 2022
0 parents commit a8b95e9
Show file tree
Hide file tree
Showing 469 changed files with 25,096 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: PHP Composer

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4', '8.0', '8.1']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: php lint
run: vendor/bin/php-cs-fixer fix --stop-on-violation .
- name: php unit test
run: vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
*.cache
build/
57 changes: 57 additions & 0 deletions .phan/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* This configuration will be read and overlaid on top of the
* default configuration. Command-line arguments will be applied
* after this file is read.
*/
return [
// Supported values: `'5.6'`, `'7.0'`, `'7.1'`, `'7.2'`, `'7.3'`,
// `'7.4'`, `'8.0'`, `'8.1'`, `null`.
// If this is set to `null`,
// then Phan assumes the PHP version which is closest to the minor version
// of the php executable used to execute Phan.
//
// Note that the **only** effect of choosing `'5.6'` is to infer
// that functions removed in php 7.0 exist.
// (See `backward_compatibility_checks` for additional options)
'target_php_version' => '7.4',

// A list of directories that should be parsed for class and
// method information. After excluding the directories
// defined in exclude_analysis_directory_list, the remaining
// files will be statically analyzed for errors.
//
// Thus, both first-party and third-party code being used by
// your application should be included in this list.
'directory_list' => [
'src',
'tests',
'vendor',
'examples',
],

// A regex used to match every file name that you want to
// exclude from parsing. Actual value will exclude every
// "test", "tests", "Test" and "Tests" folders found in
// "vendor/" directory.
'exclude_file_regex' => '@^vendor/.*/(tests?|Tests?)/@',

// A directory list that defines files that will be excluded
// from static analysis, but whose class and method
// information should be included.
//
// Generally, you'll want to include the directories for
// third-party code (such as "vendor/") in this list.
//
// n.b.: If you'd like to parse but not analyze 3rd
// party code, directories containing that code
// should be added to both the `directory_list`
// and `exclude_analysis_directory_list` arrays.
'exclude_analysis_directory_list' => [
'vendor/'
],
'suppress_issue_types' => [
'PhanUndeclaredMethod'
]
];
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# CHANGELOG

## 1.0.0 - 2022-05-19
- First release
210 changes: 210 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# Binance Connector PHP

This is a thin library that working as a connector to the Binance public API.


## Installation

```php

composer require binance/binance-connector-php

```

## How to use

```php

require_once 'vendor/autoload.php';

$client = new \Binance\Spot();
$response = $client->time();
echo json_encode($response);


$client = new \Binance\Spot($key, $secret);
$response = $client->account();
echo json_encode($response);
```

Please find `examples` folder for more endpoints

### Testnet

The [spot testnet](https://testnet.binance.vision/) is available. In order to test on testnet:

```php

$client = new \Binance\Spot([
'baseURL' => 'https://testnet.binance.vision'
]);
```

### RecvWindow

From Binance API, recvWindow is available for all endpoints require signature. By default, it's 5000ms. You are allowed to set this parameter to any value less than 60000, number beyond this limit will receive error from Binance server.

```php

$client = new \Binance\Spot($key, $secret);
$response = $client->getOrder('BNBUSDT', [
'orderId' => '11',
'recvWindow' => 10000
]
);

```

### Optional parameters

For the optional parameters in the endpoint, pass exactly the field name from API document into the optional parameter array. e.g

```php

$response = $client->cancelOCOOrder('BNBUSDT',
[
'orderListId' => '12'
]
);

```

The mandartory parameter is validated in the library level, missing required parameter will throw `Binance\Exception\MissingArgumentException`.

### Timeout

Time out in seconds.

```php

$client = new \Binance\Spot(['timeout' => 0.5]);

$response = $client->time();

echo json_encode($response);

```

### Display meta info

Binance API server returns weight usage in the header of each response. This is very useful to indentify the current usage. To reveal this value, simpily intial the client with show_weight_usage=True as:

```php

$client = new \Binance\Spot(['showWeightUsage' => true]);
$response = $client->time();
echo json_encode($response);
```

this will returns:

```json
{"data":{"serverTime":1590579807751},"weight_usage":{"x-mbx-used-weight":["2"],"x-mbx-used-weight-1m":["2"]}}
```

It's also able to print out all headers, which may be very helpful for debug:

```php

$client = new \Binance\Spot(['showHeader' => true]);
$response = $client->time();
echo json_encode($response);
```
the returns will be like:

```json

{"data":{"serverTime":1590579942001},"header":{"Content-Type":["application/json;charset=utf-8"],"Transfer-Encoding":["chunked"],...}}
```

## Websocket

```php

$client = new \Binance\Websocket\Spot();

$callbacks = [
'message' => function($conn, $msg){
echo $msg.PHP_EOL;
},
'ping' => function($conn, $msg) {
echo "received ping from server".PHP_EOL;
}
];

$client->aggTrade('btcusdt', $callbacks);

```

It's able to provide a customlized websocket connector.

```php

$loop = \React\EventLoop\Factory::create();
$reactConnector = new \React\Socket\Connector($loop);
$connector = new \Ratchet\Client\Connector($loop, $reactConnector);
$client = new \Binance\Websocket\Spot(['wsConnector' => $connector]);

$callbacks = [
'message' => function($conn, $msg){
echo "received message".PHP_EOL;
},
'pong' => function($conn) {
echo "received pong from server".PHP_EOL;
},
'ping' => function($conn) {
echo "received ping from server".PHP_EOL;
},
'close' => function($conn) {
echo "receive closed.".PHP_EOL;
}
];

$client->miniTicker('btcusdt', $callbacks);

# send ping to server intervally
$loop->addPeriodicTimer(2, function () use ($client) {
$client->ping();
echo "ping sent ".PHP_EOL;
});

$loop->run();

```

Listen to combined stream:
```php

$client->combined([
'btcusdt@miniTicker',
'ethusdt@miniTicker'
], $callbacks);

```

## Test

```shell

# install the packages
composer install

vendor/bin/phpunit
```

## Limitation
Futures and Vanilla Options APIs are not supported:

- /fapi/*
- /dapi/*
- /vapi/*
- Associated Websocket Market and User Data Streams


## Contributing
Contributions are welcome.
If you've found a bug within this project, please open an issue to discuss what you would like to change.
If it's an issue with the API, please open a topic at Binance Developer Community

## License
MIT
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "binance/binance-connector-php",
"description": "A thin layer for Binance API in PHP",
"homepage": "https://github.com/binance/binance-connector-php",
"type": "library",
"minimum-stability": "stable",
"license": "MIT",
"authors": [
{
"name": "Binance",
"email": "contact@binance.com"
}
],
"require": {
"php": ">=7.4.0",
"psr/log": "^1.1",
"guzzlehttp/guzzle": "^7.4",
"ratchet/pawl": "^0.4"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"2pd/guzzle-http-mock": "^4.0",
"friendsofphp/php-cs-fixer": "^3.0",
"phan/phan": "5.x"
},
"autoload": {
"psr-4": {
"Binance\\": "src/Binance/"
}
},
"autoload-dev": {
"psr-4": {
"Binance\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit",
"test-coverage": "XDEBUG_MODE=coverage phpunit --coverage-html=build/artifacts/coverage",
"lint-fix": "php-cs-fixer fix --using-cache=no src/",
"analyze": "phan --allow-polyfill-parser"
}

}
Loading

0 comments on commit a8b95e9

Please sign in to comment.