Hi everyone!
I am trying to create a PHP script to place STOP_LOSS_LIMIT orders, but when I try to make the call, the server responds with a Bad Request, 401 error. However, I can place MARKET orders without any issues.
Here is the code for the MARKET order:
$endpoint = ‘https://api4.binance.com/api/v3/order’; //I’ve tried all the endpoints in the guide
$params = [
‘symbol’ => $currency,
‘side’ => ‘BUY’,
‘type’ => ‘MARKET’,
‘quoteOrderQty’ = (float) number_format($maxBalanceUsable, 4),
‘timestamp’ => time() * 1000
];
$query_string = http_build_query($params);
$signature = hash_hmac(‘sha256’, $query_string, $api_secret);
$params[‘signature’] = $signature;
$headers = [
'X-MBX-APIKEY: ’ . $api_key
];
$options = [
‘http’ => [
‘header’ => implode(“\r\n”, $headers),
‘method’ => ‘POST’,
‘content’ => http_build_query($params)
]
];
$context = stream_context_create($options);
$response = file_get_contents($endpoint, false, $context);
$data = json_decode($response, true);
And here is the code for the STOP_LOSS_LIMIT order:
$endpoint = ‘https://api4.binance.com/api/v3/order’;
$params = [
‘symbol’ => $symbol, //cryptocurrency that supports STOP_LOSS_LIMIT es. SOLUSDT
‘side’ => ‘BUY’,
‘type’ => ‘STOP_LOSS_LIMIT’,
‘quantity’ => (float) number_format($currencyQuantityToBuy, 2),
‘stopPrice’ => (float) number_format($stopLossPrice, 2),
‘timestamp’ => time() * 1000
];
$query_string = http_build_query($params);
$signature = hash_hmac(‘sha256’, $query_string, $api_secret);
$params[‘signature’] = $signature;
$headers = [
'X-MBX-APIKEY: ’ . $api_key
];
$options = [
‘http’ => [
‘header’ => implode(“\r\n”, $headers),
‘method’ => ‘POST’,
‘content’ => http_build_query($params)
]
];
$context = stream_context_create($options);
$response = file_get_contents($endpoint, false, $context);
$data = json_decode($response, true);
Could you please point out where I am going wrong or how I can successfully make the call?
thank you!