I'm trying to set up Tropo's WebAPI with a Drupal site, and I thought I'd just create a hook_menu() callback for example.com/tropo, then use the hook_menu callback I just created to consume the API call from Tropo (which contains a simple JSON array of data), then respond.

I can do the response part pretty easily... build the data, drupal_json_output() and exit (that just returns the JSON basically).

The part I'm having trouble with is getting the data that is sent to the /tropo menu callback... I can't access any data by using either of the following:

$json = file_get_contents("php://input"); // returns NULL

(below uses the inputstream module:)

$json = file_get_contents("drupal://input"); // also returns NULL

I checked what some other modules do, and it looks like they use hook_init to grab the JSON. Is this the preferred method? I'd love to be able to do it in hook_menu and use my own path and such...

Alternatively, would I be better off writing my own PHP script and running outside of Drupal, just doing a bootstrap for the database if I need to?

  • Which data would the menu callback get, and what is passing those data to the menu callback (an external server, or something else)? Can you control the data format?
    – apaderno
    Jun 6, 2011 at 20:14
  • From: php.net/manual/en/wrappers.php.php: "php://input allows you to read raw data from the request body. In case of POST requests, it preferrable to $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data"." Is that maybe relevant to you?
    – Berdir
    Jun 6, 2011 at 21:22
  • I have tried php://input, but that seems to get nothing. When I'm using inputstream, drupal://input returns '1' :( Jun 7, 2011 at 3:04
  • @kiamlaluno - I'm trying to get the data posted to the URL /tropo from the Tropo servers; they submit some JSON containing a caller's phone number, and I need to respond to it. But I can't get at the JSON, so I can't get at the phone number :( Jun 7, 2011 at 3:05

2 Answers 2


Well, it turns out I was getting the data, but I forgot to decode it...

In my hook_menu callback:

$received = file_get_contents("drupal://input"); // Use inputstream module.
$received = json_decode($received, TRUE);

Then I can play around as much as I'd like with the $received value.

Additionally, the inputstream module is invaluable in letting me get to the input stream more than once (if you try using php://input more than once, the second time, and those following, will return NULL).

As an additional note, I just noticed today a new module that might be an even simpler way of handling this: Content as JSON

  • 4
    Two years later, and my Google searches bring me back full-circle. Aug 10, 2013 at 4:25
  • How do you handle authentication of who is making the request (sending the JSON) in this situation? Dec 23, 2014 at 23:07
  • Here is a bit more explanation of the solution presented above. wunderkraut.com/blog/… Dec 23, 2014 at 23:43

The REST Server that is part of the Services module does this automatically for JSON as well as for other formats: http://drupal.org/project/services

It also supports Inputstream (Inputstream was built to handle both OAuth verification and Services decoding of the same stream).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.