2

I posted JSON array from my script using jQuery.post , to get the posted data in my drupal module function I am using

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

but is there a better way or a 'drupal-way' to get posted data in a drupal function?

1 Answer 1

2

Assuming that you're using Drupal 7, take a look at the module Content as JSON. This should take care of the details and put you within the realm of the "drupal-way". There's also the Services module which might be overkill for what you're doing.

If you stick with file_get_contents then don't forget json_decode:

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

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

Additionally, the inputstream module is invaluable in letting you 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).

1
  • not working with "drupal://input", its working with "php://input"
    – Firdous
    Commented Oct 13, 2011 at 13:54

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.