80

I have to make a PATCH request using PhP cURL. I couldn't find any documentation, so I tried the following but it isn't working.

$data = "{'field_name': 'field_value'}";
$url = "http://webservice.url";
$headers = array('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);
curl_close($curl);

Any idea why this isn't working? How can I fix it?

Edit: I am connecting to a RESTful web service. It returns HTTP/1.1 200 for successful requests. Unsuccessful requests return HTTP/1.1 403. I keep getting 403.

I tried changing $data to:

$data = "data={'field_name': 'field_value'}";

It didn't change the outcome.

Edit2: The final working code.

$data = "{'field_name': 'field_value'}";
$url = "http://webservice.url";
$headers = array('Content-Type: application/json');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
mattbasta
  • 13,492
  • 9
  • 47
  • 68
Sandeep M
  • 2,942
  • 2
  • 13
  • 17

4 Answers4

141

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH'); should do it.

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
  • 3
    Thank you. This along with an additional request header `Content-Type: application/json` did the trick. – Sandeep M Jan 22 '13 at 13:07
  • for $headers = array('Content-Type: application/json'); I get : Warning: curl_setopt(): You must pass either an object or an array with the CURLOPT_HTTPHEADER argument – naman1994 Nov 07 '19 at 11:40
1

JSON PATCH would be better for data format since this format is designed for HTTP PATCH request. See https://www.rfc-editor.org/rfc/rfc6902 for the spec. The tutorial of Rails 4 show the example(http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#http-patch).

// https://www.rfc-editor.org/rfc/rfc6902#section-4
$data = '{ "op": "add", "path": "/a/b/c", "value": "foo" }';
$headers = array('Content-Type: application/json-patch+json');
Community
  • 1
  • 1
masakielastic
  • 4,540
  • 1
  • 39
  • 42
-1

Try Using normal array

//$data = "{'field_name': 'field_value'}";

$data = array('field_name' => 'field_value' );

Shridhar
  • 168
  • 1
  • 5
-1

You can define methods and use everything in one curl function. Hope this helps you.

define('GI_HTTP_METHOD_GET', 'GET');
define('GI_HTTP_METHOD_POST', 'POST');
define('GI_HTTP_METHOD_PUT', 'PUT');
define('GI_HTTP_METHOD_PATCH', 'PATCH');

curl_setopt($ch, CURLOPT_POST, true);
if ($method === GI_HTTP_METHOD_PUT) {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, GI_HTTP_METHOD_PUT);
}
if ($method === GI_HTTP_METHOD_PATCH) {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, GI_HTTP_METHOD_PATCH);
} 
Vinit Kadkol
  • 1,221
  • 13
  • 12