Doubt about changing leverage, BTCUSDT, via API - PHP

Dear,
Doubt about changing leverage, BTCUSDT

Change Initial Leverage (TRADE)
POST /fapi/v1/leverage (HMAC SHA256)

When trying to change the leverage to 21X, the following PHP code does not get the return response.

{
“leverage”: 21,
“maxNotionalValue”: “1000000”,
“symbol”: “BTCUSDT”
}

Please see my code:

//////////////////////////////////

$key = “XXXXXXXXX”;
$secret = “YYYYYYYYY”;

$timestamp = time()*1000;

$leverage_parametri = ‘symbol=BTCUSDT&leverage=21’;
$sign = hash_hmac(‘sha256’, $leverage_parametri."&timestamp=".$timestamp, $secret);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://fapi/binance.com/fapi/v1/leverage?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $leverage_parametri.”&timestamp=".$timestamp."&signature=".$sign);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Content-Type: application/x-www-form-urlencoded”,"X-MBX-APIKEY: ".$key));

$ch = curl_exec($ch);
curl_close($ch);

echo $ch;

//////////////////////////////////

What could be wrong?

The solution was…

I put in the code the instruction “curl_error($ch);”
Thus, an error was presented in the resolution of the URL.

The URL has been changed from:
https://fapi/binance.com/fapi/v1/leverage?

For:
https://www.binance.com/fapi/v1/leverage?
With this new URL the code worked!

Thanks,

What’s the error message instead of the expected response?
You need to know the occurred error to have a start point for debugging, find the reason and then correct.

1 Like

aisling2

Thanks for the comeback…
The only return I have is in the PHP log…

PHP warning: curl_close() expects parameter 1 to be a resource, bool given in

P.S.:
But the instructions: $ch = curl_init() , curl_exec($ch) and curl_close($ch) were created correctly.

//////////////////////////////////

$key = “XXXXXXXXX”;
$secret = “YYYYYYYYY”;
$timestamp = time()*1000;
$leverage_parametri = ‘symbol=BTCUSDT&leverage=21’;
$sign = hash_hmac(‘sha256’, $leverage_parametri."&timestamp=".$timestamp, $secret);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://www.binance.com/fapi/v1/leverage?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $leverage_parametri.”&timestamp=".$timestamp."&signature=".$sign);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Content-Type: application/x-www-form-urlencoded”,"X-MBX-APIKEY: ".$key));
$response = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ’ . curl_error($ch);
}
curl_close($ch);
echo “Return from leverage change $response”;

//////////////////////////////////