Binance Connector PHP - a thin library that working as a connector to the Binance public API

Overview

Binance Connector PHP

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

Installation

composer require binance/binance-connector-php

How to use

require_once 'vendor/autoload.php';

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


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

Please find examples folder for more endpoints

Testnet

The spot testnet is available. In order to test on testnet:

$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.

$client = new \Binance\Spot(['key' => $key, 'secret' => $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

$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.

$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:

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

this will returns:

{"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:

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

the returns will be like:

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

Websocket

aggTrade('btcusdt', $callbacks); ">
$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.

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(); ">
$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:

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

Test

# 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

Comments
  • composer require binance/binance-connector-php issue

    composer require binance/binance-connector-php issue

    composer require binance/binance-connector-php Could not find a version of package binance/binance-connector-php matching
    your minimum-stability (stable). Require it with an explicit version constr
    aint allowing its desired stability.

    can you tell me the dependence i should install. (php 7.4.30 installed)

    opened by esonbest 5
  • subAccount list query stopped working since 2022-09-20 20:00:00 UTC

    subAccount list query stopped working since 2022-09-20 20:00:00 UTC

    I am constantly monitoring new sub-users on hourly base via GET https://api.binance.com/sapi/v1/sub-account/list?page=1&limit=500&recvWindow=5000&timestamp=CURRENT_TIMESTAMP&signature=VALID_SIGNATURE. Since 2022-09-20 20:00:00 UTC it stopped working, resulting in the following output:

    Fatal error: Uncaught Binance\Exception\ClientException: GuzzleHttp\Exception\ClientException: Client error: GET https://api.binance.com/sapi/v1/sub-account/list?page=1&limit=500&recvWindow=5000&timestamp=CURRENT_TIMESTAMP&signature=VALID_SIGNATURE resulted in a 400 Bad Request response: {"code":-10038,"msg":"Broker account is not supported."} in /MYPATH/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /MYPATH/vendor/guzzlehttp/guzzle/src/Middleware.php(69): GuzzleHttp\Exception\RequestException::create() #1 /MYPATH/vendor/guzzlehttp/promises/src/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp{closure}() #2 /MYPATH/vendor/guzzlehttp/promises/src/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler() #3 /MYPATH/vendor/guzzlehttp/promises/src/TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise{closure}() #4 /MYPATH/vendor/guzzlehttp/promises/src/Promise.php(248): GuzzleHttp\Promise\TaskQueue->run() #5 /MYPATH/vendor/guzzlehttp/promises/src/Promise.php(224): GuzzleHttp\Promise\Promise->invokeWaitFn() #6 /MYPATH/vendor/guzzlehttp/promises/src/Promise.php(269): GuzzleHttp\Promise\Promise->waitIfPending() #7 /MYPATH/vendor/guzzlehttp/promises/src/Promise.php(226): GuzzleHttp\Promise\Promise->invokeWaitList() #8 /MYPATH/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #9 /MYPATH/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\Promise\Promise->wait() #10 /MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php(104): GuzzleHttp\Client->request() #11 /MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php(98): Binance\APIClient->processRequest() #12 /MYPATH/vendor/binance/binance-connector-php/src/Binance/Spot/SubAccount.php(48): Binance\APIClient->signRequest() #13 /MYPATH/subuserminitor.php(28): Binance\Spot->subAccountList() #14 {main} in /MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php:106 Stack trace: #0 /MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php(98): Binance\APIClient->processRequest() #1 /MYPATH/vendor/binance/binance-connector-php/src/Binance/Spot/SubAccount.php(48): Binance\APIClient->signRequest() #2 /MYPATH/subuserminitor.php(28): Binance\Spot->subAccountList() #3 {main} thrown in /MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php on line 106

    Please help with the issue.

    opened by arsmagic 1
  • subAccountUniversalTransferHistory (Master account) is not functioning

    subAccountUniversalTransferHistory (Master account) is not functioning

    GET https://api.binance.com/sapi/v1/sub-account/universalTransfer?page=1&recvWindow=5000&timestamp=1663519465973&signature=XXX results in in a 400 Bad Request response: {"code":100001002,"msg":"Unsupported operation"} in MYPATH/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 MYPATH/vendor/guzzlehttp/guzzle/src/Middleware.php(69): GuzzleHttp\Exception\RequestException::create() #1 MYPATH/vendor/guzzlehttp/promises/src/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp{closure}() #2 MYPATH/vendor/guzzlehttp/promises/src/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler() #3 MYPATH/vendor/guzzlehttp/promises/src/TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise{closure}() #4 MYPATH/vendor/guzzlehttp/promises/src/Promise.php(248): GuzzleHttp\Promise\TaskQueue->run() #5 MYPATH/vendor/guzzlehttp/promises/src/Promise.php(224): GuzzleHttp\Promise\Promise->invokeWaitFn() #6 MYPATH/vendor/guzzlehttp/promises/src/Promise.php(269): GuzzleHttp\Promise\Promise->waitIfPending() #7 MYPATH/vendor/guzzlehttp/promises/src/Promise.php(226): GuzzleHttp\Promise\Promise->invokeWaitList() #8 MYPATH/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #9 MYPATH/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\Promise\Promise->wait() #10 MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php(104): GuzzleHttp\Client->request() #11 MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php(98): Binance\APIClient->processRequest() #12 MYPATH/vendor/binance/binance-connector-php/src/Binance/Spot/SubAccount.php(553): Binance\APIClient->signRequest() #13 MYPATH/test4.php(14): Binance\Spot->subAccountUniversalTransferHistory() #14 {main} in MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php:106 Stack trace: #0 MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php(98): Binance\APIClient->processRequest() #1 MYPATH/vendor/binance/binance-connector-php/src/Binance/Spot/SubAccount.php(553): Binance\APIClient->signRequest() #2 MYPATH/test4.php(14): Binance\Spot->subAccountUniversalTransferHistory() #3 {main} thrown in MYPATH/vendor/binance/binance-connector-php/src/Binance/APIClient.php on line 106

    XXX - the real signature is substituted, as well as MYPATH - is for the original URL/URI/path masquerading.

    reviewing 
    opened by arsmagic 1
  • release v1.3.0

    release v1.3.0

    Added

    • New endpoints for Portfolio Margin:
      • GET /sapi/v1/portfolio/pmLoan to query Portfolio Margin Bankruptcy Loan Record.
      • POST /sapi/v1/portfolio/repay to repay Portfolio Margin Bankruptcy Loan.
      • GET /sapi/v1/portfolio/collateralRate to get Portfolio Margin Collateral Rate.
    opened by 2pd 1
  • Fatal error after PHP update

    Fatal error after PHP update

    Fatal error: Uncaught Error: Class "Psr\Log\NullLogger" not found in XXX/vendor/binance/binance-connector-php/src/Binance/APIClient.php:73 Stack trace: #0 XXX/vendor/binance/binance-connector-php/src/Binance/Spot.php(35): Binance\APIClient->__construct() #1 MYscript: Binance\Spot->__construct() #2 {main} thrown in XXX/vendor/binance/binance-connector-php/src/Binance/APIClient.php on line 73

    Any idea, what is the cause for the error? Just updated PHP 7.4 to PHP v8.1, updated and re-installed all the packages, and the previously working script still causes such an error....

    reviewing 
    opened by arsmagic 1
  • Rc-1.2.0

    Rc-1.2.0

    New endpoint for Margin:

    • POST /sapi/v3/asset/getUserAsset to get user assets.

    New endpoint for Wallet:

    • GET /sapi/v1/margin/dribblet to query the historical information of user's margin account small-value asset conversion BNB.

    Update endpoint for Convert:

    • GET /sapi/v1/fiat/orders: Weight changes from IP(3000) to IP(100)
    opened by tantialex 1
Releases(v1.4.0)
  • v1.4.0(Dec 20, 2022)

  • v1.3.0(Sep 14, 2022)

    Added

    • New endpoints for Portfolio Margin:
      • GET /sapi/v1/portfolio/pmLoan to query Portfolio Margin Bankruptcy Loan Record.
      • POST /sapi/v1/portfolio/repay to repay Portfolio Margin Bankruptcy Loan.
      • GET /sapi/v1/portfolio/collateralRate to get Portfolio Margin Collateral Rate.
    Source code(tar.gz)
    Source code(zip)
  • v1.2.0(Jul 19, 2022)

    Added

    New endpoint for Margin:

    • POST /sapi/v3/asset/getUserAsset to get user assets.

    New endpoint for Wallet:

    • GET /sapi/v1/margin/dribblet to query the historical information of user's margin account small-value asset conversion BNB.

    Changed

    Update endpoint for Convert:

    • GET /sapi/v1/fiat/orders: Weight changes from IP(3000) to IP(100)
    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Jul 7, 2022)

    Add

    • New endpoint GET /api/v3/ticker
    • New endpoint POST /api/v3/order/cancelReplace
    • New websocket stream <symbol>@ticker_<window_size>
    • New websocket stream !ticker_<window-size>@arr

    Update

    • Pump guzzlehttp/guzzle to latest version
    • Apply lint fix
    Source code(tar.gz)
    Source code(zip)
  • v1.0.2(Jun 14, 2022)

  • v1.0.1(Jun 4, 2022)

  • v1.0.0(May 19, 2022)

Owner
Binance
Open source, from Binance with <3
Binance
This is a simple php ADP client / connector

This is a simple php ADP client / connector

null 4 Dec 4, 2022
Job Queue Public API PHP Client

Job Queue API PHP Client PHP client for the Job Queue API (API docs). Usage composer require keboola/job-queue-api-php-client use Keboola\JobQueueClie

Keboola 0 Dec 20, 2021
A simple PHP API to make working with SharePoint lists easy.

PHP SharePoint Lists API The PHP SharePoint Lists API is designed to make working with SharePoint Lists in PHP a less painful developer experience. Ra

Carl Saggs 170 Dec 10, 2022
It's a PHP Application to simplify working with Google Sheets SDK for php.

About GoogleSheetsPHP It's a PHP Application to simplify working with Google Sheets SDK for php. Note: i used Slim 3 to construct the application but

Sami Alateya 5 Dec 20, 2022
OpenAI API Client is a component-oriented, extensible client library for the OpenAI API. It's designed to be faster and more memory efficient than traditional PHP libraries.

OpenAI API Client in PHP (community-maintained) This library is a component-oriented, extensible client library for the OpenAI API. It's designed to b

Mounir R'Quiba 6 Jun 14, 2023
Lightweight PHP library for WhatsApp API to send the whatsapp messages in PHP provided by ultramsg.com

Ultramsg.com WhatsApp API PHP SDK Lightweight PHP library for WhatsApp API to send the whatsappp messages in PHP provided by Ultramsg.com Installation

Ultramsg 117 Dec 26, 2022
Google-api-php-client - A PHP client library for accessing Google APIs

Google APIs Client Library for PHP Reference Docs https://googleapis.github.io/google-api-php-client/main/ License Apache 2.0 The Google API Client Li

Google APIs 8.4k Dec 30, 2022
Wise-php - This library is written to accommodate the wise API's use in php projects With Wise

Wise-php - This library is written to accommodate the wise API's use in php projects With Wise you can automate payments, connect your business tools, and create ways to manage your finances. You can also power your cross-border and domestic payouts.

Albert Xhani 15 Nov 17, 2022
BeckhoffPLCSoapClient - SoapClient to communicate with BeckHoff PLC. Library made in PHP based on TcAdsWebService JavaScript Library.

BeckhoffPLCSoapClient - SoapClient to communicate with BeckHoff PLC. Library made in PHP based on TcAdsWebService JavaScript Library.

null 3 May 18, 2022
PHP library for the Stripe API.

Stripe PHP bindings The Stripe PHP library provides convenient access to the Stripe API from applications written in the PHP language. It includes a p

Stripe 3.3k Jan 5, 2023
A PHP library for communicating with the Twilio REST API and generating TwiML.

twilio-php The default branch name for this repository has been changed to main as of 07/27/2020. Documentation The documentation for the Twilio API c

Twilio 1.4k Jan 2, 2023
A PHP library for the Campaign Monitor API

createsend A PHP library which implements the complete functionality of the Campaign Monitor API. Installation Composer If you use Composer, you can r

Campaign Monitor 287 Jan 6, 2023
PHP 5.3+ library which helps you to interact with the DigitalOcean API

DigitalOcean The version 2 of the API will be available soon ! Please visit DigitalOceanV2 and contribute :) This PHP 5.3+ library helps you to intera

Antoine Kirk 156 Jul 30, 2022
PHP library for the GitHub API v3

GitHub API v3 - PHP Library Currently under construction. Overview Provides access to GitHub API v3 via an Object Oriented PHP library. The goal of th

Darren Rees 62 Jul 28, 2022
PHP library to use IOTA REST API to help node management and tangle queries

iota.php About PHP library to use IOTA REST API to help node management and tangle queries. Please be aware that this library is in an early developme

IOTA Community 45 Dec 13, 2022
PHP library for the ArvanCloud API

PHP ArvanCloud API PHP library for the ArvanCloud API. This package supports PHP 7.3+. For Laravel integration you can use mohammadv184/arvancloud-lar

Mohammad Abbasi 5 Apr 29, 2022
A library written in PHP to interact with Coinbase Pro via API.

A library written in PHP to interact with Coinbase Pro via API.

Blake Hamilton 4 Mar 31, 2022
Upload Vimeo video with CodeIgniter, Official PHP library for the Vimeo API

Upload Vimeo video with CodeIgniter, Official PHP library for the Vimeo API. Vimeo Video upload with API using Official PHP library for the Vimeo API.

WordPress theme and Plugins developers 2 Oct 10, 2021
The library provides convenient access to the Epoint.az API from applications written in the PHP language.

Tural/Epoint library/SDK The library provides convenient access to the Epoint.az API from applications written in the PHP language. It includes a pre-

Tural 5 Jan 20, 2022