How do I make a PATCH request in PHP using cURL?

How do I make a PATCH request in PHP using cURL?

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

Stefan Bogdanescu

Founder & Senior Architect

The X-HTTP-Method-Override Pattern and JSON Payloads

When attempting to execute a PATCH request in PHP, specifically when the underlying server does not natively support the HTTP PATCH method, developers often resort to using the X-HTTP-Method-Override header. This pattern allows a client to send an actual POST or PUT request while instructing the server to interpret it as a different operation. However, the approach outlined in your source material contains a critical architectural flaw that explains why you are receiving HTTP 403 Forbidden responses instead of the expected success codes.

The primary issue lies in how the payload is being transmitted. In your example, you are sending $data as a raw string containing {'field_name': 'field_value'}. While this might look like JSON to the human eye, it is semantically incorrect for modern RESTful services. Standard JSON requires double quotes around keys and string values. Furthermore, even if the server ignores the header and treats your request as a standard POST, sending raw unencoded strings in CURLOPT_POSTFIELDS often results in application/x-www-form-urlencoded or plain text content types, rather than application/json.

Most robust RESTful APIs expect JSON payloads for PATCH operations. When you send a malformed string like {'field_name': 'field_value'}, the server likely rejects it because the content type is wrong or the syntax violates JSON standards. This results in a 403 error, which indicates that the request was received but the method or payload is not authorized or valid for the resource.

To fix this, you must ensure two things: the correct HTTP verb simulation and a properly formatted JSON body. If your server strictly requires the X-HTTP-Method-Override header because it doesn't support PATCH, you should send a POST request with that header and a valid JSON payload. Here is how to structure the code correctly:

$data = json_encode(['field_name' => 'field_value']);
$url = "http://webservice.url";
$headers = [
    'Content-Type: application/json',
    'X-HTTP-Method-Override: PATCH'
];

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

if ($httpCode === 200) {
    echo "Success";
} else {
    echo "Failed with code: " . $httpCode;
}

The critical change here is json_encode(). This function ensures the output is valid JSON with double quotes and proper escaping. Additionally, explicitly setting the Content-Type header to application/json signals to the server that you are sending structured data, not form fields or plain text. Without this header, many frameworks will default to expecting form-encoded data, leading to parsing errors on the server side.

If you cannot change the server configuration to support native PATCH requests, relying on X-HTTP-Method-Override is a viable fallback. However, it adds complexity and potential for misconfiguration. It is worth noting that many modern PHP frameworks abstract this away entirely. For instance, if you are building or consuming APIs within an application stack, using the LaravelCompany ecosystem would allow you to leverage built-in HTTP clients that handle method overrides gracefully without manually managing cURL options.

Another potential pitfall in your original code is the data format inside the string. The source material shows $data = "{'field_name': 'field_value'}". Even if you switch to json_encode, ensuring the keys are quoted is essential. Single quotes inside a double-quoted string in PHP do not automatically convert to JSON syntax; they remain single quotes until explicitly processed.

Finally, always inspect the HTTP response code using curl_getinfo(). A 403 status is ambiguous; it could mean "Forbidden" (insufficient permissions) or "Method Not Allowed" (if the override header is ignored). Checking the specific error message returned by the API in the response body will clarify whether the issue was the JSON syntax, the missing content type, or an actual authentication failure.

Our network