Search code examples
phpapirestbinancebinance-api-client

Binance API - 1101 - duplicated value for Symbol parameter


I'm trying to call Binance API via PHP code. But I'm getting the following response

{"code":-1101,"msg":"Duplicate values for parameter 'symbol'."}

Even though there is no duplicated value inside the symbol param, its not even an array, here is the code ->

    $apiKey = '';
    $apiSecret = '';
    
    $url = 'https://api.binance.com/api/v3/order/test';
    $data = [
        'symbol' => 'BTCUSDT',
        'side' => self::SIDE_BUY,
        'type' => 'MARKET',
        'quantity' => 0.01,
    ];
    $timestamp = time()*1000; //get current timestamp in milliseconds
    
    $query_str = [];
    $query_str = $data;
    $query_str['timestamp'] = $timestamp;
    $query_str = http_build_query($query_str);
    $signature = hash_hmac('sha256', $query_str, $apiSecret);
    $query_str .= "&signature=" . $signature;

    //Decide content type
    $content_type = ["Content-Type: application/x-www-form-urlencoded","X-MBX-APIKEY: ".$apiKey];

    // Final url
    $url .= '?' . $query_str;

    $ch = @curl_init();
    @curl_setopt($ch, CURLOPT_HEADER, false);
    @curl_setopt($ch, CURLOPT_URL, $url);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    @curl_setopt($ch, CURLOPT_HTTPHEADER, $content_type);
    @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    @curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $query_str);

    $response = @curl_exec($ch);
    $http_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
    @curl_close($ch);

Solution

  • at the last part of your code you build your final $url, but $url will be separate from other options/parameters... In the way you do it now, symbol and any other parameter will be posted twice.

    So remove

    $url .= '?' . $query_str;

    and the error is gone!

    Hope this will work out fine for you.