Search code examples
phpmysqlphpstormslim

Cannot get response from PUT request using Slim Framework


Am trying to update an existing entity in my database using PUT request but i have 2 issues, when am calling the request from phpStorm rest client debugger am getting an error

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

when i call the same request from Advance rest client addon on google chrome am getting

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

so i can't understand if the real bug is on the verifyRequiredParams or in my function implementation . Am providing the code if someone can help me.

This is my index.php file

$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);
});

This is the function code for the verifyRequiredParams which is located in the index.php file

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();
}
}

And this is my DbHandler.php file where the function is located.

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

All my connections are ok i have checked them and also my other services are working fine


Solution

  • The two errors was because advance rest client use "PUT" with capital letters and php storm with lower characters even if its written with capital in php storm i notice that by changing "PUT" with "put" in the if statement of verifyRequiredParams function and now its working and updating perfectly