We have already seen how to write GET and POST methods in a Slim web service, and also how to send GET and POST requests from a PHP client. However, you probably remember that REST uses two other methods too: PUT and DELETE. How can we develop code to make use of these methods?
Handling DELETE requests with a Slim application is easy. We simply write delete and put routes in exactly the same way as we would write get and post routes, by using the delete and put methods of our \Slim\App object. For example:
$app->delete('/flights/{id}', function ($req, $res, array $args) { $stmt = $this->db->prepare("DELETE FROM flights WHERE id=?"); $stmt->bindParam (1, $args["id"]); $stmt->execute(); });This example sets up a DELETE route for the relative URL /flights/{id}, e.g. /flights/45678. If this URL receives a DELETE request, the flight with the corresponding ID will be deleted from the database.
With a PUT request, we need to send new data (typically as JSON) to our web service. Our web service then needs to parse that JSON and use it to update the database. Note that calling getBody()->getContents(); on the Request object will retrieve the contents of the body of the HTTP request:
$app->put('/flights/{id}', function ($req, $res, array $args) { $put = json_decode($req->getBody()->getContents(), true); $stmt = $this->db->prepare("UPDATE flights SET depart=?, arrive=?, price=? WHERE id=?"); $stmt->execute([$put["depart"], $put["arrive"], $put["price"], $args["id"]]); });Note a couple of things about this example:
Web services would normally be called from a client script or application, for example with cURL. You have already seen how to use cURL to setup GET and POST requests. You can also use cURL to send a DELETE request by adding the line:
curl_setopt($curl,CURLOPT_CUSTOMREQUEST, "DELETE");PUT requests are a bit more difficult as you have to write a temporary file on your server and send that file over to the web service. To easily handle the different types of HTTP request in REST applications, I have written a wrapper function, call_web_service(), with which you can easily make a request to a REST web service. You can download it here (ZIP file); the source code is also viewable online here. To use it in a script, you need to add the line:
include('call_web_service.php');at the top of your script.
With this script, to call any web service all you need to do is:
$result = call_web_service( URL, method, data);
http://hittastic.com/song/1009you would send some JSON to update the details of song 1009. This JSON would be parsed in our Slim web service as we saw in the PUT example above.
$result = call_web_service("https://edward2.solent.ac.uk/~pjones/flight/1009", "DELETE");This would send a DELETE request to the URL, causing flight 1009 to be deleted.
$newData = [ "depart" => "0900", "arrive" => "1200", "price" => 200.0 ]; $result = call_web_service("https://edward2.solent.ac.uk/~pjones/flight/1009", "PUT", json_encode($newData));This would send a PUT request to the URL, causing flight 1009 to be modified. The associative array $newData is encoded into JSON and sent within the body of the request. We then use the method described in "Server side", above, to parse the data sent via the PUT request with Slim.
The return value of call_web_service(), $result in the examples above, is an associative array containing the result returned from the web service. It contains two fields:
/review/{id}and delete the review with the given ID.