8

php://input is working properly in localhost. But in server it returns empty. Input( request ) to my site is a json(REST - application/json type), so $_POST didn't work ( please read This question ) .

$_POST works with key-value pair type inputs like form-data or x-www-urlencoded

key1=value1&key2=value2&key3=value3

I'm using application/json as input (in REST).

Like {'key1':'value1','key2':'value2','key3':'value3'}

This can't be handled using $_POST. But using php://input can help to read that data.

My code

class Webservice_Controller extends CI_Controller {
    public $json_input_data;
    public function __construct(){
        parent::__construct();
        $this->json_input_data = json_decode(file_get_contents('php://input'),TRUE);
    }
    public function json_input($label){
        if(isset($this->json_input_data[$label])) return $this->json_input_data[$label];
        else return NULL;
    }
}

Above code is works fine in another webserver also, But not in the current one. :(

I think my web server deny access to php://input.

Is there is any other methods to read json input in php ?

4
  • first things to check, is the php versions the same, and if allow_fopen_url is set Commented Aug 22, 2013 at 4:35
  • Try var_dump($this->json_input_data ) in __construct() Commented Aug 22, 2013 at 4:39
  • @RohanKumar -> it prints "NULL" in server i hosted. Prints "array(2) { ["id"]=> string(2) "79" ["t"]=> string(5) "22_00" }" in localhost. Commented Aug 22, 2013 at 7:44
  • @DevZer0 PHP version is 5.3 and it supports. I didn't find an option to access php.ini file in my control panel ( it's a shared server ). Is there any method to test that ? Plz help.. Commented Aug 22, 2013 at 7:51

2 Answers 2

4

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $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".

See wrappers

Try this too,

....
  public function __construct(){
    parent::__construct();
    if(isset($_POST))
    {
       var_dump(file_get_contents('php://input'));
       $this->json_input_data=json_decode(file_get_contents('php://input'),TRUE);
    }
    else echo 'Not Post';
  }
....

Also check for allow_url_fopen.

Sign up to request clarification or add additional context in comments.

2 Comments

I saw the wrappers, and also the input isn't enctype="multipart/form-data". It works fine in localhost, but the web server i used to host returns null while i using php://input. May be it denay access to that. I want to know is it possible to do in any other methods ???
it gives "NULL" in server i hosted. Prints "array(2) { ["id"]=> string(2) "79" ["t"]=> string(5) "22_00" }" in localhost. It's mentioned in comments of the question. I think 'php://input' is not accessible in that web server.
4

I know this is old, but it might help others:

Careful with single quotes inside your json

From the PHP documentation about json_decode:

the name and value must be enclosed in double quotes
single quotes are not valid 

$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.