Curl 和 PHP - 如何通过 PUT、POST、GET 通过 curl 传递 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21271140/
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
Curl and PHP - how can I pass a json through curl by PUT,POST,GET
提问by Gilberg
I have been working on building an Rest API for the hell of it and I have been testing it out as I go along by using curl from the command line which is very easy for CRUD
我一直在努力为它构建一个 Rest API,并且我一直在测试它,我一直在通过从命令行使用 curl 来测试它,这对 CRUD 来说非常容易
I can successfully make these call from the command line
我可以从命令行成功进行这些调用
curl -u username:pass -X GET http://api.mysite.com/pet/1
curl -d '{"dog":"tall"}' -u username:pass -X GET http://api.mysite.com/pet
curl -d '{"dog":"short"}' -u username:pass -X POST http://api.mysite.com/pet
curl -d '{"dog":"tall"}' -u username:pass -X PUT http://api.mysite.com/pet/1
The above calls are easy to make from the command line and work fine with my api, but now I want to use PHP to create the curl. As you can see, I pass data as a json string. I have read around and I think I can probably do the POST and include the POST fields, but I have not been able to find out how to pass http body data with GET. Everything I see says you must attached it to the url, but it doesn't look that way on the command line form. Any way, I would love it if someone could write the correct way to do these four operations in PHP here on one page. I would like to see the simplest way to do it with curl and php. I think I need to pass everything through the http body because my php api catching everything with php://input
上面的调用很容易从命令行进行,并且可以很好地与我的 api 一起使用,但现在我想使用 PHP 来创建 curl。如您所见,我将数据作为 json 字符串传递。我已经阅读了周围,我想我可以执行 POST 并包含 POST 字段,但是我无法找到如何使用 GET 传递 http 正文数据。我看到的一切都说你必须将它附加到 url,但它在命令行表单上看起来不是那样。无论如何,如果有人能在一页上写出在 PHP 中执行这四个操作的正确方法,我会很高兴的。我想看看用 curl 和 php 做的最简单的方法。我想我需要通过 http 主体传递所有内容,因为我的 php api 使用 php://input 捕获所有内容
回答by voodoo417
PUT
放
$data = array('username'=>'dog','password'=>'tall');
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
POST
邮政
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
GETSee @Dan H answer
GET见@Dan H 回答
DELETE
删除
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
回答by brickpop
You can use this small library: https://github.com/ledfusion/php-rest-curl
你可以使用这个小库:https: //github.com/ledfusion/php-rest-curl
Making a call is as simple as:
拨打电话非常简单:
// GET
$result = RestCurl::get($URL, array('id' => 12345678));
// POST
$result = RestCurl::post($URL, array('name' => 'John'));
// PUT
$result = RestCurl::put($URL, array('$set' => array('lastName' => "Smith")));
// DELETE
$result = RestCurl::delete($URL);
And for the $result variable:
而对于 $result 变量:
- $result['status'] is the HTTP response code
- $result['data'] an array with the JSON response parsed
- $result['header'] a string with the response headers
- $result['status'] 是 HTTP 响应代码
- $result['data'] 解析了 JSON 响应的数组
- $result['header'] 带有响应头的字符串
Hope it helps
希望能帮助到你
回答by Dan H
For myself, I just encode it in the url and use $_GET on the destination page. Here's a line as an example.
就我自己而言,我只是在 url 中对其进行编码,然后在目标页面上使用 $_GET。这里以一行为例。
$ch = curl_init();
$this->json->p->method = "whatever";
curl_setopt($ch, CURLOPT_URL, "http://" . $_SERVER['SERVER_NAME'] . $this->json->path . '?json=' . urlencode(json_encode($this->json->p)));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
EDIT: Adding the destination snippet... (EDIT 2 added more above at OPs request)
编辑:添加目标代码段...(在 OP 请求时在上面添加了更多的 EDIT 2)
<?php
if(!isset($_GET['json']))
die("FAILURE");
$json = json_decode($_GET['json']);
$method = $json->method;
...
?>
回答by Luiz Vaz
I was Working with Elastic SQL plugin. Query is done with GET method using cURL as below:
我正在使用Elastic SQL 插件。查询是通过使用 cURL 的 GET 方法完成的,如下所示:
curl -XGET http://localhost:9200/_sql/_explain -H 'Content-Type: application/json' \
-d 'SELECT city.keyword as city FROM routes group by city.keyword order by city'
I exposed a custom port at public server, doing a reverse proxy with Basic Auth set.
我在公共服务器上公开了一个自定义端口,使用基本身份验证集进行反向代理。
This code, works fine plus Basic Auth Header:
这段代码,加上 Basic Auth Header 可以正常工作:
$host = 'http://myhost.com:9200';
$uri = "/_sql/_explain";
$auth = "john:doe";
$data = "SELECT city.keyword as city FROM routes group by city.keyword order by city";
function restCurl($host, $uri, $data = null, $auth = null, $method = 'DELETE'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host.$uri);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($method == 'POST')
curl_setopt($ch, CURLOPT_POST, 1);
if ($auth)
curl_setopt($ch, CURLOPT_USERPWD, $auth);
if (strlen($data) > 0)
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$resp = curl_exec($ch);
if(!$resp){
$resp = (json_encode(array(array("error" => curl_error($ch), "code" => curl_errno($ch)))));
}
curl_close($ch);
return $resp;
}
$resp = restCurl($host, $uri); //DELETE
$resp = restCurl($host, $uri, $data, $auth, 'GET'); //GET
$resp = restCurl($host, $uri, $data, $auth, 'POST'); //POST
$resp = restCurl($host, $uri, $data, $auth, 'PUT'); //PUT
回答by Jignesh Gothadiya
set one more property curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
再设置一个属性 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);

![php 警告:PDO::__construct(): [2002] 中没有这样的文件或目录(试图通过 unix:///tmp/mysql.sock 连接)](/res/img/loading.gif)