php用curl的post方法传递json包的时候,接受方是怎么获取的呢

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php用curl的post方法传递json包的时候,接受方是怎么获取的呢相关的知识,希望对你有一定的参考价值。

参考技术A $postData = file_get_contents('php://input');

这个在我的博客《PHP cURL实现模拟登录与采集使用方法详解》里“十一发送与获取json数据”
已经做了详细的介绍,包括如何获取和发送json数据。

参考:http://www.zjmainstay.cn/php-curl

参考技术B 接收post请求附带的数据。

Curl和PHP - 如何通过PUT,POST,GET将json传递给curl

我一直在努力为它构建一个Rest API,我一直在测试它,因为我从命令行使用curl这很容易进行CRUD

我可以从命令行成功进行这些调用

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

上面的调用很容易从命令行生成,并与我的api工作正常,但现在我想使用PHP来创建卷曲。如您所见,我将数据作为json字符串传递。我已经阅读过,我认为我可以做POST并包含POST字段,但我无法找到如何通过GET传递http正文数据。我看到的一切都说你必须将它附加到网址上,但它在命令行表单上看起来并不那样。无论如何,如果有人能在一页上用PHP编写正确的方法来完成这四项操作,我会很高兴。我想看看用curl和php做最简单的方法。我想我需要通过http主体传递所有内容,因为我的php api用php://输入捕获所有内容

答案

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

GET见@Dan H回答

删除

$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);
另一答案

你可以使用这个小库:https://github.com/jmoraleda/php-rest-curl

拨打电话很简单:

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

对于$ result变量:

  • $ result ['status']是HTTP响应代码
  • $ result ['data']一个解析了JSON响应的数组
  • $ result ['header']带有响应头的字符串

希望能帮助到你

另一答案

对于我自己,我只是在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);

编辑:添加目标片段...(在OPs请求时,EDIT 2在上面添加了更多内容)

<?php
if(!isset($_GET['json']))
    die("FAILURE");
$json = json_decode($_GET['json']);
$method = $json->method;
...
?>
另一答案

我和Elastic SQL plugin一起工作。使用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'

我在公共服务器上公开了一个自定义端口,使用Basic Auth set进行反向代理。

此代码,正常工作和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
另一答案

再设置一个属性curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);

以上是关于php用curl的post方法传递json包的时候,接受方是怎么获取的呢的主要内容,如果未能解决你的问题,请参考以下文章

Curl 和 PHP - 如何通过 curl 通过 PUT、POST、GET 传递 json

Curl和PHP - 如何通过PUT,POST,GET将json传递给curl

如何向php服务器发送数据为json的post请求

php post 设置header json传参

php curl get 参数

PHP模拟POST提交数据并获得返回值之CURL方法(使用PHP extension,然后使用php_curl.dll,很不错)