JSON / Google Code POST Mystery Revealed

So I must have spent six (not kidding at all) hours today figuring this out, so I think it’s worth posting up here in case it spares anyone else similar OCDishness. I was working with Google Code today, trying to see what is what, and I came across a service called PostCommitWebHooks, which uses a new technology called Web Hooks for sites to update one another when something mutually-interesting happens. It employs an update mechanism called JSON (XML I hardly knew ye!) with HMAC hashes and whatever else, basically I was way out of my depth.

But this Google support page seemed so straightforward and clear-eyed about the whole thing that I thought it can’t be that bad. Of course the examples are in Python and Java and I wanted to use PHP but this individual was trying it too a month back and well ok.

So I got the thing set up, and Google’s site was talking to my site, and passing me the special HTTP_GOOGLE_CODE_PROJECT_HOSTING_HOOK_HMAC variable (not important). But the problem was the $_POST variables I was getting were empty. As in zero. And $_REQUEST didn’t have it, and there were no $_FILES, so where was the JSON? And that’s when I googled some a lot more and came across a thing I’ve never ever seen in all Seven Years of coding PHP, which is the existence of something called raw POST data. All you need is this:

file_get_contents(“php://input”)

This will show you the JSON data that Google is posting to your page. I know. I make it seem so easy!

posted: March 3, 2009

3 Responses to “JSON / Google Code POST Mystery Revealed”

  1. Nandeep 4/10/2009 at 9:45 pm

    Wow! This had me stumped for hours! Thanks for posting this! Kudos!

    I can truly relate to your feelings. I could never have figured that out.

  2. joshr 4/13/2009 at 7:08 pm

    Cheers to you sir! I’m glad my little post was of service!

  3. Deadpan110 12/10/2009 at 12:43 am

    Thankyou… I had just set up some php to collect $_POST data and found nothing… spotted your link on the Google page and it would seem you have saved me 6 hours work also!!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>