0

Over the last few weeks I've been learning about iOS development, which has naturally led me into the world of APIs. Now, searching around on the Internet, I've come to the conclusion that using the REST architecture is very much recommended, due to its supposed simplicity and ease of implementation.

However, I'm really struggling with the implementation side of REST. I understand the concept; using HTTP methods as verbs to describe the action of a request and responding with suitable response codes, and so on. It's just, I don't understand how to code it.

I don't get how I map a URI to an object. I understand that a GET request for domain.com/api/user/address?user_id=999 would return the address of user 999 - but I don't understand where or how that mapping from /user/address to some method that queries a database has taken place.

Is this all coded in one PHP script? Would I just have a method that grabs the URI like so:

$array = explode("/", ltrim(rtrim($_SERVER['REQUEST_URI'], "/"), "/"))

And then cycle through that array, first I would have a request for a "user", so the PHP script would direct my request to the user object and then invoke the address method. Is that what actually happens?

The main thing I'm not getting is how that URI /user/address?id=999 somehow is broken down and executed - does it actually resolve to code?

class user(id) {
   address() {
     //get user address
   }
}
3
  • There's no magic way to do it automatically. It doesn't have to be one big script, but you need a way to route requests to the relevant code. This could be done with mod_rewrite in Apache (or similar redirecting modules in other web servers), or via an index.php that looks at the REQUEST_URI and calls the appropriate code. Commented Jan 16, 2011 at 13:26
  • weierophinney.net/matthew/archives/… -- An example of one way of doing it, do note there are many many ways to achieve the same end. Commented Jan 16, 2011 at 17:34
  • Just a comment, but I feel your URI scheme is a bit mixed up comapred to what I would expect. A Better example would be like this: http://domain.com/api/user/99/address where you get more specific going left to right. Commented May 9, 2011 at 3:00

3 Answers 3

1

Actually the API you're trying to describe now is not RESTful. There are many sources describing how to make RESTful APIs. So you should first define your API (taking in account how your client software would use it) and then implement it. I'm quite sure that almost any RESTful API can be implemented in PHP.

Here are some other tips on how to make a RESTful API.

In my opinion GlassFish Server REST Interface is a good example of RESTful design.

Sign up to request clarification or add additional context in comments.

Comments

1

That's two questions.

To honor RESTful HTTP verbs, you have to query $_SERVER["REQUEST_METHOD"]. It will contain the usual GET or POST unless a more specialized HTTP request was received. The whole REST buzz is somewhat misleading in itself, and also in misusing the HTTP verbs just for routing.

Anyway, the mapping of request URLs to functions can be achieved in two ways. It's most reliable to use a static map, for example an array that lists URL patterns and destination methods. Most PHP frameworks use an implicit mapping like so:

$args = explode("/", trim($_SERVER['REQUEST_URI'], "/"));
$class = array_shift($args);
$method = array_shift($args);
call_user_func_array("$class::$method", $args);

Note how this is a bad example, security-wise. Only allowed and specifically prepared classes and methods should be able to receive requests. Most frameworks just check if it was derived from an acceptable base class after loading it from a known path and/or instantiating. But a static map is really preferable.

Anyway, regular expression mapping or handling by mod_rewrite is also common. And for utilizing HTTP verbs, just include it as method name.

Comments

0

Have a look at FRAPI - http://getfrapi.com/

As it says focus on your business logic, not presentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.