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

Gil*_*erg 53 php post curl get put

我一直在努力为它构建一个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
Run Code Online (Sandbox Code Playgroud)

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

voo*_*417 146

$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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)


小智 11

您可以使用这个小型库: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); 
Run Code Online (Sandbox Code Playgroud)

对于$ result变量:

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

希望能帮助到你


Dan*_*n H 5

对于我自己,我只是在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);
Run Code Online (Sandbox Code Playgroud)

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

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