Skip to content

Commit

Permalink
Merge pull request #10 from jaggedsoft/master
Browse files Browse the repository at this point in the history
Add error messages
  • Loading branch information
Jon Eyrick committed Jan 23, 2018
2 parents b0a5862 + ed531e6 commit faee4e5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Packagist Downloads](https://img.shields.io/packagist/dt/jaggedsoft/php-binance-api.svg)](https://packagist.org/packages/jaggedsoft/php-binance-api)

# PHP Binance API
This project is designed to help you make your own projects that interact with the [Binance API](https://www.binance.com/restapipub.html). 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.
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.

#### Installation
```
Expand Down
30 changes: 26 additions & 4 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ private function request($url, $params = [], $method = "GET") {
];
$context = stream_context_create($opt);
$query = http_build_query($params, '', '&');
return json_decode(file_get_contents($this->base.$url.'?'.$query, false, $context), true);
try {
$data = file_get_contents($this->base.$url.'?'.$query, false, $context);
} catch ( Exception $e ) {
return ["error"=>$e->getMessage()];
}
return json_decode($data, true);
}

private function signedRequest($url, $params = [], $method = "GET") {
Expand All @@ -129,8 +134,16 @@ private function signedRequest($url, $params = [], $method = "GET") {
$query = http_build_query($params, '', '&');
$signature = hash_hmac('sha256', $query, $this->api_secret);
$endpoint = $base.$url.'?'.$query.'&signature='.$signature;
$data = file_get_contents($endpoint, false, $context);
return json_decode($data, true);
try {
$data = file_get_contents($endpoint, false, $context);
} catch ( Exception $e ) {
return ["error"=>$e->getMessage()];
}
$json = json_decode($data, true);
if ( isset($json['msg']) ) {
echo "signedRequest error: {$data}".PHP_EOL;
}
return $json;
}

private function apiRequest($url, $method = "GET") {
Expand All @@ -143,7 +156,12 @@ private function apiRequest($url, $method = "GET") {
]
];
$context = stream_context_create($opt);
return json_decode(file_get_contents($this->base.$url, false, $context), true);
try {
$data = file_get_contents($this->base.$url, false, $context);
} catch ( Exception $e ) {
return ["error"=>$e->getMessage()];
}
return json_decode($data, true);
}

public function order($side, $symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
Expand Down Expand Up @@ -178,6 +196,10 @@ public function candlesticks($symbol, $interval = "5m") {
private function balanceData($array, $priceData = false) {
if ( $priceData ) $btc_value = $btc_total = 0.00;
$balances = [];
if ( empty($array) || empty($array['balances']) ) {
echo "balanceData error: Please make sure your system time is synchronized, or pass the useServerTime option.".PHP_EOL;
return [];
}
foreach ( $array['balances'] as $obj ) {
$asset = $obj['asset'];
$balances[$asset] = ["available"=>$obj['free'], "onOrder"=>$obj['locked'], "btcValue"=>0.00000000, "btcTotal"=>0.00000000];
Expand Down

0 comments on commit faee4e5

Please sign in to comment.