[英]Cannot get response from PUT request using Slim Framework
我正在嘗試使用 PUT 請求更新我的數據庫中的現有實體,但我有兩個問題,當我從 phpStorm rest 客戶端調試器調用請求時出現錯誤
{"error":true,"message":"Required field(s) restaurant_id, service_rating, food_rating, music_rating is missing or empty"}
當我在谷歌瀏覽器上調用來自高級休息客戶端插件的相同請求時
{"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”和帶有小寫字符的 phpstorm,即使它在 phpstorm 中用大寫我注意到,通過在 verifyRequiredParams 函數的 if 語句中將“PUT”更改為“put”和現在它的工作和更新完美
在 index.php 文件中添加波紋管代碼
$app->addBodyParsingMiddleware();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.