Php 使用带有查询参数的cURL进行RESTAPI PUT调用
实际上,我正试图借助cURL在PHP应用程序中进行RESTAPI PUT调用 当我运行以下命令时,使用“邮递员”;它执行得很好Php 使用带有查询参数的cURL进行RESTAPI PUT调用,php,rest,curl,Php,Rest,Curl,实际上,我正试图借助cURL在PHP应用程序中进行RESTAPI PUT调用 当我运行以下命令时,使用“邮递员”;它执行得很好 http://localhost:3000/testApi/updateTestComment?commentId=1&comment=test { "Error": false, "Message": "Success", "Comments": { "fieldCount": 0, "affectedRows": 1,
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");
}
如果您将数据发布为多部分/表单数据,那么您的请求内容类型是否应为application/json?对于PUT use
curl\u setopt($curl,CURLOPT\u PUT,true)代码>