This is part II of my series on Ajax and REST. For more info, see
part I. In part I, I said that I would flesh out the AJAX/REST interplay. Unfortunately, before I am able to do that, I have to lay in some groundwork.
I'm good with the PHP. I know my way around the language. Because I want
Fuchi to be coded up quickly, I am using PHP for it. Now, I would really rather do it in
scheme, but there doesn't seem to be much in the way of intarweb scheme goodness.
So I tried to implement a PUT handler in PHP. My god. The pain.
Apparently PHP before 4.3 threw an HTTP error 405 (Request method not allowed) when it encountered a PUT or DELETE (or quite possibly an OPTIONS or TRACE). Post 4.3, PHP handles PUT and DELETE requests, but to tell you the truth, I don't know where it stashes the variables. I don't think that the superglobal $_REQUEST is populated. And I am sure that there is no $_PUT or $_DELETE. (Attn: PHP Devs. MAKE IT GO! ;) )
So I have had to build my own handler for PUT and DELETE requests. I'll be posting a link a little later on to the class. Its pretty basic, but that is the whole point, a basic, standard interface that you can use to write RESTful applications.
If you want to play with REST and PHP, and don't want to wait for me to post my code (or even just don't want to use it...) I'll give you a few pointers. Obviously there is the $_SERVER['REQUEST_METHOD'] superglobal that gives you, of all things, the request method. Form data from the request is sent in the body of the request, usually with an accompanying content-type. The input stream works for getting access to that data. According to the manual:
php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives.
Of course, the format of this data depends on the content-type. There really is only 2 applicable types.
This should help you out in parsing the form data. So the trick is to open the php://input stream for reading, and parse the data.
Update: Fixed some bad markup. Sorry 'bout that.
Last night I was able to put the finishing touches on Meditation, the PHP REST API Framework. This is a simple set of 2 classes to facilitate the building of RESTful PHP applications. As I mentioned in a previous entry "The Third Taste... Not So
Tracked: Jan 10, 11:29