簡體   English   中英

Curl 和 PHP - 如何通過 PUT、POST、GET 通過 curl 傳遞 json

[英]Curl and PHP - how can I pass a json through curl by PUT,POST,GET

我一直在努力為它構建一個 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 捕獲所有內容

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

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/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'] 帶有響應頭的字符串

希望能幫助到你

就我自己而言,我只是在 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);

編輯:添加目標代碼段...(在 OP 請求時在上面添加了更多的 EDIT 2)

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

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

我在公共服務器上公開了一個自定義端口,使用基本身份驗證集進行反向代理。

這段代碼,加上 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM