poetix

this time for sure

PHP/REST Part I: Methods and URLs

Following the advice given in this article, I set up Apache to treat /triplespace (and all dependent URLs, e.g./triplespace/foo/bar) as PHP files. So now I have some PHP that will display the HTTP method with which it was called, and the complete path requested by the client:

<?php

header("Content-Type: text/plain");

$METHOD=$_SERVER['REQUEST_METHOD'];

$URI=$_SERVER['REQUEST_URI'];

echo("Method is '$METHOD'/n");

echo("Uri is $URI");

?>

As far as REST is concerned, that’s a large part of the battle. I had feared that recourse to mod_rewrite might be necessary; it looks as if it won’t be.

Next step was to test whether PUT and DELETE requests were getting through. For this purpose, I wrote a little Python script:

from httplib import HTTPConnection

h1 = HTTPConnection("localhost", 80)

h1.request("PUT", "/triplespace/foo")

response1 = h1.getresponse()

print response1.read()

h2 = HTTPConnection("localhost", 80)

h2.request("DELETE", "/triplespace/foo")

response2 = h2.getresponse()

print response2.read()

Which worked fine. The last stage was to upload the PHP and .htaccess files to Dreamhost, and try it there. To see the results, go to http://www.codepoetics.com/triplespace/hooray - or try pointing the bit of Python code above to it…