Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use curl #2

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,43 @@ public function balances($priceData = false) {
}

private function request($url, $params = [], $method = "GET") {
$opt = [
"http" => [
"method" => $method,
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\n"
]
];
$context = stream_context_create($opt);
$query = http_build_query($params, '', '&');
return json_decode(file_get_contents($this->base.$url.'?'.$query, false, $context), true);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,
"User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\n"
);

$url = $this->base.$url.'?'.$query;

if (!empty($queryString)) {
$url .= '?'.$queryString;
}

if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
}

curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$response = curl_exec($ch);

return json_decode($response, true);
}

private function signedRequest($url, $params = [], $method = "GET") {
$base = $this->base;
$opt = [
"http" => [
"method" => $method,
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\nX-MBX-APIKEY: {$this->api_key}\r\n"
]
];
$context = stream_context_create($opt);


$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,
"User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\nX-MBX-APIKEY: {$this->api_key}\r\n"
);

$params['timestamp'] = number_format(microtime(true)*1000,0,'.','');
if ( isset($params['wapi']) ) {
unset($params['wapi']);
Expand All @@ -114,7 +131,17 @@ 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}";
return json_decode(file_get_contents($endpoint, false, $context), true);

if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
}

curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$response = curl_exec($ch);
return json_decode($response, true);
}

private function apiRequest($url, $method = "GET") {
Expand Down