PHP CURL DELETE request

PHP CURL DELETE request

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why curl_setopt(CURLOPT_POSTFIELDS) Breaks DELETE Requests in PHP

When working with REST APIs, handling the HTTP DELETE method can be surprisingly finicky if you aren't careful about how you configure cURL. It is a common frustration to see GET and POST requests succeed flawlessly while a DELETE request fails immediately with an "HTTP internal server error" or 405 Method Not Allowed.

The root cause often lies in how PHP's cURL extension interprets the CURLOPT_POSTFIELDS option. In your specific case, you are setting CURLOPT_POSTFIELDS to an empty string (derived from json_encode("")) even though the DELETE method typically expects no request body.

The Misuse of CURLOPT_POSTFIELDS

The primary issue in your implementation is the inclusion of CURLOPT_POSTFIELDS. When you set this option, cURL attempts to attach a request body to the outgoing request. For GET and POST methods, this is often necessary or expected. However, for DELETE requests, sending a body is frequently unsupported by servers, and more importantly, it can cause the server to reject the request entirely if it expects a strict adherence to the HTTP specification where DELETE implies an empty payload.

By passing $data (which might be an empty string "") to CURLOPT_POSTFIELDS, you are explicitly telling cURL to send headers like Content-Length: 0 and potentially Content-Type. While some servers tolerate this, many strict REST APIs will return a 405 Method Not Allowed or 500 Internal Server Error because they do not expect a body on a DELETE request.

The Correct Approach for DELETE

To fix this, you must remove the CURLOPT_POSTFIELDS option entirely when invoking the DELETE method. If your API requires specific headers for a DELETE operation (such as an Authorization token), those should be handled via CURLOPT_HTTPHEADER, not through the fields option.

Here is how the logic should be restructured to ensure the server receives a clean DELETE request without an unexpected body:

public function curl_delete($path, $token) {
    $ch = curl_init($this->__url . $path);
    
    // Set the custom request method to DELETE
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    
    // Ensure we get the response back as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    // Set headers if necessary (e.g., Authorization)
    $headers = [
        'Authorization: Bearer ' . $token,
        'Accept: application/json'
    ];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    // CRITICAL: Do NOT set CURLOPT_POSTFIELDS for DELETE requests
    // unless the API documentation explicitly states a body is required.
    
    $result = curl_exec($ch);
    
    if ($result === false) {
        return ['error' => 'cURL failed: ' . curl_error($ch)];
    }
    
    return json_decode($result, true);
}

public function deleteUser($extid, $token) {
    // Ensure the path is correctly formatted for DELETE. 
    // Many APIs expect the token in the header, not appended to the URL path.
    $path = "/rest/user/" . $extid; 
    
    return $this->curl_delete($path, $token);
}

Handling Authentication and Path Construction

Another potential pitfall in your original code was the URL construction: /rest/user/".$extid."/;token=".$this->__token. Appending a token directly to the query string of the path (using ; or ?) is generally discouraged for security reasons. Tokens, especially if they are sensitive credentials, should almost always be passed via the Authorization header as demonstrated above.

If your specific API documentation requires the token in the URL, ensure that you are not mixing query parameters with a request body. The combination of a malformed body and a non-standard URL structure often confuses the server's routing logic, leading to the internal server error you observed.

Final Configuration Check

Before blaming the network or the server configuration, double-check your cURL initialization. Ensure that CURLOPT_HTTPAUTH is not interfering if the server expects basic auth but receives none, or vice versa. However, in 90% of cases involving this specific pattern, simply removing the CURLOPT_POSTFIELDS line resolves the conflict between what the client sends and what the server expects for a DELETE operation.

Our network