【问题标题】:Curl and PHP - how can I pass a json through curl by PUT,POST,GETCurl 和 PHP - 如何通过 curl 通过 PUT、POST、GET 传递 json
【发布时间】:2014-02-11 19:33:42
【问题描述】:

我一直致力于为它构建一个 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 来创建 curl。如您所见,我将数据作为 json 字符串传递。我已经阅读过,我想我可能可以执行 POST 并包含 POST 字段,但我无法找出如何使用 GET 传递 http 正文数据。我看到的所有内容都说您必须将其附加到 url,但在命令行表单上看起来并不是这样。无论如何,如果有人可以在一页上用 PHP 编写执行这四个操作的正确方法,我会很高兴。我想看看用 curl 和 php 做的最简单的方法。我认为我需要通过 http 正文传递所有内容,因为我的 php api 使用 php://input

捕获所有内容

【问题讨论】:

    标签: php post curl get put


    【解决方案1】:

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

    发布

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

    获取 见@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);
    

    【讨论】:

    • 感谢 PUT,如果您可以添加 GET(正文中带有 json)和 POST,我会接受
    • @Gilberg 为 POST 添加。在 GET 请求中,数据通过 url 传递 :)
    • 谢谢,我不能使用 Dan 的,因为我的 api 正在等待 php://input,如果我将它包含在 url 中,它将不会转到 php://input 流。简而言之,我无法从 $_GET 中获取它,我可以,但这会有点让人头疼。我可以使用curl -d '{json-string}' -X GET http://api.mysite.com/user 从命令行轻松地做到这一点,所以必须有一种方法可以在 php 中模仿它
    • 谢谢@voodoo4q7 我会接受,但我仍然想看看如何在 GET 正文中传递数据。我知道我的服务器接受它,所以这对我来说不是问题。
    • 你也可以使用 CURLOPT_PUT 吗?
    【解决方案2】:

    你可以使用这个小库:https://github.com/ledfusion/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'] 带有响应头的字符串

    希望对你有帮助

    【讨论】:

      【解决方案3】:

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

      编辑:添加目标 sn-p...(编辑 2 应 OP 请求在上面添加了更多内容)

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

      【讨论】:

      • 谢谢,您能否在 php 中写出完整的 curl 代码,以明确您如何定义 POST、GET、PUT、DELETE。我的 API 会查找这些方法。
      • 已添加。这只是一个简单的 HTTP 请求,但正如您在目的地上看到的那样,我使用 GET。它实际上只是移动一个 json 字符串(例如,从我的应用程序前端到 script1,再到 script2,然后将其回复回应用程序前端 - 不包含在源代码中)。
      • +1,但我将等待 POST、PUT 和 DELETE,因为我编写的其余 api 需要这些
      【解决方案4】:

      我正在使用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'
      

      我在公共服务器上公开了一个自定义端口,使用基本身份验证集进行反向代理。

      这段代码,加上基本身份验证头,工作正常:

      $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
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 这是个坏建议。设置该标志意味着您没有验证 SSL 证书。它使您面临 DNS 劫持。
        猜你喜欢
        • 2015-04-02
        • 2016-02-22
        • 1970-01-01
        • 2019-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多