PHP: Get POST JSON

By  on  

My recent work at Mozilla has me creating an OAuth-like authentication transaction between Bugzilla and Phabricator.  This task has thrust me back into the world of PHP, a language I haven't touched much (since version ~5.2) outside of creating WordPress themes and plugins for this blog.  Coming back to a language you haven't touched in years feels like a completely new experience; you notice patterns and methods that you wouldn't have guessed of in years past.

Part of the authentication transaction requires Phabricator to receive a POST request that contains JSON data.  I had expected the data to land in $_POST but the variable was empty; how the hell do I get the POST data?  To get POST JSON with PHP, you use the following:

# Get JSON as a string
$json_str = file_get_contents('php://input');

# Get as an object
$json_obj = json_decode($json_str);

file_get_contents, which I though was only used to retrieve content from local files or traditional URLs, allows you to use the special php://input address to retrieve JSON data as a string.  From there you use json_decode to turn the JSON string into a workable object/array.

It makes sense that the JSON isn't handled via normal $_POST since there's really no key, per se; essentially you just need the "blob" of data as a whole, which is provided by php://input.  You can test the JSON+POST handling with cURL.

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    Web Audio API

    The Web Audio API allows developers to load and decode audio on demand using JavaScript.  The more I evaluate awesome games for Firefox OS TVs, the more I get to learn about these APIs that I normally wouldn't touch.  The following is a very basic introduction to the WebAudio API...

  • By
    Create Your Own Dijit CSS Theme with LESS CSS

    The Dojo Toolkit seems to just get better and better.  One of the new additions in Dojo 1.6 was the use of LESS CSS to create Dijit themes.  The move to using LESS is a brilliant one because it makes creating your own Dijit theme...

Discussion

  1. Lorenzo

    I always use this line of code:

    @json_decode(($stream = fopen('php://input', 'r')) !== false ? stream_get_contents($stream) : "{}");

    It does not throw a warning in case there’s no request body, and simply fallback to an empty object instead of null.

  2. @David, you might want to merge $_POST and 'php://input'

    $_POST = array_merge($_POST, (array) json_decode(file_get_contents('php://input')));
  3. Jose Maria Ferri Azorin

    You say “It makes sense that the JSON isn’t handled via normal $_POST…” but I can’t find any sense since I’m using jQuery AJAX or XMLHttpResponse calls (POST) from many years ago, parsing parameters as json or plain text and parameters, in server, are where I expect they are: $_POST array… for me, using file_get_contents('php://input') is a non-logic behaviour

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!