如何使用 cURL 在 PHP 中发出 PATCH 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14451401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a PATCH request in PHP using cURL?
提问by Sandeep M
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);
回答by quickshiftin
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');should do it.
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');应该这样做。
回答by Shridhar
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');
回答by masakielastic
JSON PATCH would be better for data format since this format is designed for HTTP PATCH request. See http://tools.ietf.org/html/rfc6902for 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');
回答by Vinit Kadkol
You can define methods and use everything in one curl function. Hope this helps you.
您可以在一个 curl 函数中定义方法并使用所有内容。希望这对你有帮助。
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);
}

