Parsing PUT requests in PHP
I’m working on the next generation data access layer for Digg right now, which is basically a REST layer built on top of a partitioned and multihomed database setup. The general idea is that we’ll send GET, POST, PUT and DELETE requests to URI’s on our services layer to access and manipulate data. PHP makes accessing GET and POST easy via $_GET and $_POST. DELETE isn’t an issue since what we’re deleting is just the entity defined by the URI (e.g. Sending DELETE to /2.0/User/1234.xml will delete User 1234).
After a few days work I can create, fetch and delete entities from this setup. Today I started working on implementing the PUT method. I always knew PHP wasn’t exactly top notch when it came to PUT support, but I had no idea how annoying it would be to find a simple solution for parsing PUT information. After some digging around this is what I’ve figured out.
$put = array();
parse_str(file_get_contents('php://input'), $put);
That should parse everything into a native PHP array, including arguments like foo[bar]=1&foo[baz]=2. If anyone knows of a more native way of doing this please let me know.

May 18th, 2008 at 10:17 pm
Joe, I’m curious as to how you are securing these services to make sure requests are only coming from the places you think it is.
May 19th, 2008 at 4:07 pm
We’re not. These aren’t public services.
May 21st, 2008 at 5:05 am
I think there is a PECL class that might do it. I found this in the PHP manual. Not sure if it will do what you need it to.
May 21st, 2008 at 3:19 pm
I did notice that PECL extension, but, alas, it also returns a string. I’d still have to use
parse_str()to import the data. Additionally, the method I’ve outlined doesn’t require loading another module.June 5th, 2008 at 2:14 am
This is defiantly something that needs to be addressed in future versions of PHP. Works for me, though. Thanks!