php - 无法使用 Slim Framework 从 PUT 请求获得响应

标签 php mysql phpstorm slim

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

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

当我在 google chrome 上从 Advance rest client addon 调用相同的请求时

{"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;
} 

我所有的连接都正常,我已经检查过了,我的其他服务也工作正常

最佳答案

这两个错误是因为高级休息客户端使用带有大写字母的“PUT”和带有小写字母的 php storm,即使它在 php storm 中用大写字母书写我注意到通过在 if 语句中将“PUT”更改为“put” verifyRequiredParams 功能,现在它的工作和更新完美

关于php - 无法使用 Slim Framework 从 PUT 请求获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40510148/

相关文章:

php显示来自mysql数据库的数据并分组

php - postman (试图获取非对象的属性)错误

php - 在页面重新加载时保存 javascript 复选框状态?

php - 在 PHP 中缓存?

php - MySQL 查询代码对 Web 服务的两个不同字段使用 OR

phpstorm - 在新窗口中打开最近的项目 留在后面

php 文件加载不正确显示空白页

php - GROUP BY 用作列的行上的某个值

phpstorm - 有没有办法在 PhpStorm 中监控文件更改并自动上传文件?

phpstorm - 重新缩进时如何禁用换行?