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.

5 Responses to “Parsing PUT requests in PHP”

  1. keithelder Says:

    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.

  2. joe Says:

    We’re not. These aren’t public services.

  3. Robert Gonzalez Says:

    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.

  4. joe Says:

    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.

  5. tylermenezes Says:

    This is defiantly something that needs to be addressed in future versions of PHP. Works for me, though. Thanks!

Leave a Reply

You must be logged in to post a comment.


Auto Insurance Stories