使用Slim框架无法从PUT请求获得响应


Cannot get response from PUT request using Slim Framework

我试图使用PUT请求更新数据库中的现有实体,但我有2个问题,当我从phpStorm rest客户端调试器调用请求时,我得到一个错误

{"error":true,"message":"Required field(s) restaurant_id, service_rating, food_rating, music_rating is missing or empty"}

当我调用相同的请求从高级休息客户端插件在谷歌chrome我得到

{"error":true,"message":"Task failed to update. Please try again!"}

所以我不能理解如果真正的错误是在verifyRequiredParams或在我的功能实现。我提供的代码,如果有人可以帮助我。

这是我的index.php文件

$app->put('/userRatings/:rating_id', 'authenticate', function($rating_id) use($app) {
// check for required params
verifyRequiredParams(array('restaurant_id', 'service_rating', 'food_rating', 'music_rating'));
global $user_id;
$restaurant_id = $app->request->put('restaurant_id');
$service_rating = $app->request->put('service_rating');
$food_rating = $app->request->put('food_rating');
$music_rating = $app->request->put('music_rating');

$db = new DbHandler();
$response = array();
// updating rating
$result = $db->updateRating($user_id, $rating_id, $restaurant_id, $service_rating, $food_rating, $music_rating);
if ($result) {
    // rating updated successfully
    $response["error"] = false;
    $response["message"] = "Task updated successfully";
} else {
    // task failed to update
    $response["error"] = true;
    $response["message"] = "Task failed to update. Please try again!";
}
echoRespnse(200, $response);
});

这是位于index.php文件

中的verifyRequiredParams的函数代码
function verifyRequiredParams($required_fields) {
$error = false;
$error_fields = "";
$request_params = array();
$request_params = $_REQUEST;
// Handling PUT request params
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
    $app = 'Slim'Slim::getInstance();
    parse_str($app->request()->getBody(), $request_params);
}
foreach ($required_fields as $field) {
    if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
        $error = true;
        $error_fields .= $field . ', ';
    }
}
if ($error) {
    // Required field(s) are missing or empty
    // echo error json and stop the app
    $response = array();
    $app = 'Slim'Slim::getInstance();
    $response["error"] = true;
    $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
    echoRespnse(400, $response);
    $app->stop();
}
}

这是函数所在的DbHandler.php文件。

public function updateRating( $user_id, $rating_id, $restaurant_id, $service_rating, $food_rating, $music_rating) {
    $stmt = $this->conn->prepare("UPDATE  user_ratings set  service_rating = ?, food_rating = ?, music_rating = ? WHERE user_id = ? AND rating_id = ? AND restaurant_id = ?");
    $stmt->bind_param("iiiiii", $user_id , $rating_id, $restaurant_id, $service_rating, $food_rating, $music_rating);
    $stmt->execute();
    $num_affected_rows = $stmt->affected_rows;
    $stmt->close();
    return $num_affected_rows > 0;
} 

我所有的连接都很好,我已经检查过了,我的其他服务也很好

这两个错误是因为高级rest客户端使用大写字母的"PUT"和小写字符的php storm,即使它在php storm中用大写字母写,我注意到,通过在verifyRequiredParams函数的if语句中用"PUT"改变"PUT",现在它的工作和更新完美

在index.php文件中添加以下代码

$app->addBodyParsingMiddleware();