使用带有查询参数的cURL进行REST api PUT调用

时间:2015-09-02 04:40:34

标签: php rest curl

我实际上是在cURL的帮助下尝试在PHP应用程序中进行REST api PUT调用。

使用' Postman',当我运行以下内容时;它执行得很好,

http://localhost:3000/testApi/updateTestComment?commentId=1&comment=test

{
  "Error": false,
  "Message": "Success",
  "Comments": {
    "fieldCount": 0,
    "affectedRows": 1,
    "insertId": 0,
    "serverStatus": 2,
    "warningCount": 0,
    "message": "(Rows matched: 1  Changed: 1  Warnings: 0",
    "protocol41": true,
    "changedRows": 1
  }
}

但是当我尝试在PHP应用程序中进行相同的调用时,我得到一个"无效的json"错误。我不确定自己哪里出错了,希望得到一些帮助。这是我到目前为止所做的,

$url = "http://localhost:3000/testApi/updateTestComment";
$curl = curl_init($url . "?commentId=comment_id&comment=comment_body");
$data = array(
  'comment_id' => $commentId,
  'comment_body' => $comment_body
  );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"OAuth-Token: $token"));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));

// Make the REST call, returning the result
$response = curl_exec($curl);
if (!$response) {
    die("Connection Failure.n");
}

0 个答案:

没有答案