PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Building A RESTful PHP Server: Understanding the Request

Note: This article was originally published at Planet PHP on 19 January 2012.
Planet PHP

Once upon a time, what seems like a lifetime ago, I was away for a couple of weeks, and I wrote a series of posts about serving RESTful APIs from PHP to keep my blog going while I was away. Fast forward a few years and those posts are outdated and still wildly popular - so I thought it was about time I revisited this and showed how I'm writing RESTful PHP servers today!

In the first part of this (probably) 3-part series, we'll begin with the basics. It might seem boring, but the most important thing to get right with REST is parsing all the various elements of the HTTP request and responding accordingly. I've put in code samples from from a small-scale toy project I created to make me think about the steps involved (should I put the code somewhere so you can see it? Let me know). Without further ado, let's dive in and begin by sending all requests through one bootstrap script:

Send Everything to index.php

The first thing we need to do when we serve a RESTful response is the same as the first thing we do when we serve any other web page: figure out what the user actually wanted and how to deliver that.

To support the "pretty URLs" that we see in RESTful services (actually REST is way more complicated than that, but if I start talking about that as well, you'll still be reading in 3 hours' time! Wikipedia has rather a good explanation), we'll add a .htaccess file to our directory which redirects all the requests to index.php. My .htaccess file therefore looks like this:

RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]

This assumes you're using apache with mod_rewrite; the same thing can be achieved with other webservers. With this in place, all requests to the service will arrive into the top of index.php - exactly the same way as most of the MVC (model-view-controller) frameworks work. In fact, this whole RESTful service is going to be very MVC in style, because it makes sense to me to build things this way.

That said there are definitely other options; I've also worked with Slim, which is a microframework. This is neat as it lets you register a combination of URL and verb, and specify a callback that should be triggered when a matching request comes in. It's lightweight and fast ... again, probably a post in itself if I start talking about it.

Figure Out What Was Requested

There are a few things we will want to know about what we have received here:

  • The URL of the request
  • The method (GET, POST, PUT or DELETE) of the request
  • Any data that arrived with it

Let's begin by looking at the first two elements; the code for these are common to all requests so we'll put it in index.php as all requests come here in the first instance. I'm going to store all the details about the request in a Request object, which also has the functionality needed to parse all that information. Here are the first few lines of that object:

class Request { public $url_elements; public $verb; public $parameters; A public function __construct() { $this-verb = $_SERVER['REQUEST_METHOD']; $this-url_elements = explode('/',

Truncated by Planet PHP, read more at the original (another 12783 bytes)