You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP调用GDAX-API发起DELETE请求返回400 BadRequest错误求助

Troubleshooting GDAX API 400 BadRequest on DELETE (Cancel Order)

Hey Dan, let’s break down why your order cancellation DELETE request is throwing a 400 BadRequest when all other API operations (placing orders, checking status, balance) work perfectly. Here are the most likely fixes to try:

1. Fix the Signature Data for DELETE Requests

GDAX’s signature rules are strict: for DELETE requests, the data parameter used to generate the signature must be an empty string. If you’re passing a null or empty array instead, json_encode() will turn it into "null" or "{}"—this mismatches what GDAX expects, causing a signature validation failure (and 400 error).

Update your cancelOrder method to explicitly pass an empty string as the data parameter:

public function cancelOrder($order_id) {
    // Pass empty string for DELETE request data requirement
    return $this->query('DELETE', "/orders/" . TRIM($order_id), '');
}

Also, double-check your signature() method to ensure it uses the empty string directly (not a processed version) when the HTTP method is DELETE.

2. Correct the CURL Request Method Order

Looking at your code, you set CURLOPT_CUSTOMREQUEST to DELETE before calling $this->curl_setopt($ch);. If your custom curl_setopt method resets request settings (like defaulting to GET), this will overwrite your DELETE method.

Move the DELETE option inside the switch block, after your initial curl setup:

$ch = curl_init(); 
$this->curl_setopt($ch); 
curl_setopt($ch, CURLOPT_URL, $url); 
// ... (header and signature setup) ...

switch(strtoupper($method)) { 
    case "POST": 
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
        break; 
    case "PUT": 
        curl_setopt($ch, CURLOPT_PUT, 1); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
        break; 
    case "DELETE": 
        // Set DELETE method AFTER initial curl config
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
        // Ensure no POST fields are sent (GDAX rejects this for DELETE)
        curl_setopt($ch, CURLOPT_POSTFIELDS, null);
        break; 
} 

3. Verify Order ID Validity

A 400 can also happen if the order ID you’re trying to cancel is invalid, already canceled, or filled. First, call the GET /orders/{order_id} endpoint to confirm:

  • The order exists
  • Its status is still open (only open orders can be canceled)

4. Get Detailed Error Context

The generic "BadRequest" message isn’t helpful. Enable CURL verbose logging to see the full error response from GDAX:

// Add these lines after curl_init()
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verboseLog = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verboseLog);

// After curl_exec($ch)
rewind($verboseLog);
$errorDetails = stream_get_contents($verboseLog);
echo "GDAX Error Details: " . $errorDetails;
fclose($verboseLog);

This will show you exactly what GDAX is rejecting (e.g., invalid signature, invalid order ID, etc.).

内容的提问来源于stack exchange,提问作者DanRay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:01:17