如何使用cURL在PHP中创建PATCH请求?

时间:2021-04-08 07:19:02

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.

我必须使用PhP cURL发出PATCH请求。我找不到任何文档,所以我尝试了以下但是它没有用。

$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.

编辑:我正在连接到RESTful Web服务。它为成功请求返回HTTP / 1.1 200。不成功的请求返回HTTP / 1.1 403.我一直收到403。

I tried changing $data to:

我尝试将$ data更改为:

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

It didn't change the outcome.

它没有改变结果。

Edit2: The final working code.

Edit2:最终的工作代码。

$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);

3 个解决方案

#1


63  

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

curl_setopt($ curl,CURLOPT_CUSTOMREQUEST,'PATCH');应该这样做。

#2


1  

Try Using normal array

尝试使用普通数组

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

// $ data =“{'field_name':'field_value'}”;

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

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

#3


1  

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

JSON PATCH对于数据格式会更好,因为此格式是为HTTP PATCH请求而设计的。有关规范,请参见http://tools.ietf.org/html/rfc6902。 Rails 4的教程展示了这个例子(http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#http-patch)。

// http://tools.ietf.org/html/rfc6902#section-4
$data = '{ "op": "add", "path": "/a/b/c", "value": "foo" }';
$headers = array('Content-Type: application/json-patch+json');

#1


63  

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

curl_setopt($ curl,CURLOPT_CUSTOMREQUEST,'PATCH');应该这样做。

#2


1  

Try Using normal array

尝试使用普通数组

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

// $ data =“{'field_name':'field_value'}”;

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

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

#3


1  

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

JSON PATCH对于数据格式会更好,因为此格式是为HTTP PATCH请求而设计的。有关规范,请参见http://tools.ietf.org/html/rfc6902。 Rails 4的教程展示了这个例子(http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#http-patch)。

// http://tools.ietf.org/html/rfc6902#section-4
$data = '{ "op": "add", "path": "/a/b/c", "value": "foo" }';
$headers = array('Content-Type: application/json-patch+json');