Skip to content

Commit

Permalink
[Improvement] Add basic error handling
Browse files Browse the repository at this point in the history
More improvements to come
  • Loading branch information
Jon Eyrick authored Jan 23, 2018
1 parent 165f038 commit ed531e6
Showing 1 changed file with 26 additions and 4 deletions.
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 ed531e6

Please sign in to comment.