0

I have this problem.

I used this sentence to get post info in CodeIgniter 1.7.2 and it works fine

function function1(){
    $input_data = json_decode(trim(file_get_contents('php://input')), true);
    $info = str_replace( '"', '', json_encode($input_data['info']));
}

My input json is this:

{
    "info":"hello!"
}

But when I used these same lines in CodeIgniter 2.1.3 it doesn't work. I used echo $info but my answer is null. Anyone can help me? Where is the mistake?

Rasclatt
  • 12,498
  • 3
  • 25
  • 33

2 Answers2

0

This behavior is not CI related. It's most PHP concepts. Your mistake is probably that you're trying to access something that doesn't exist.

What is the point to use file_get_contents('php://input')? Can't you just use $this->input->post(), or the uploadclass? (in CI context)?

Anyway, check what do you get in file_get_contents(), var_dumping it. I'm pretty sure that your NULL comes from $input_data assignment statment, so you're fetching the JSON in the wrong way at first.

zedee
  • 419
  • 1
  • 4
  • 15
  • The reason is because I'm using a controller as a web service, so in a post I saw that it works in a previous work. – Javier Landa-Torres Apr 22 '15 at 17:16
  • 1
    Ok now you put me in context. What do you obtain if you `var_dump(file_get_contents('php://input'))` ?? – zedee Apr 22 '15 at 23:33
  • With var_dump(file_get_contents('php://input')); Y got this -> string(0) "" – Javier Landa-Torres Apr 23 '15 at 14:34
  • Then there's the `NULL` you're getting (`json_decode` on an empty string). Problem is in the input. Check this: http://stackoverflow.com/questions/2731297/file-get-contentsphp-input-or-http-raw-post-data-which-one-is-better-to – zedee Apr 23 '15 at 16:58
0

I'm using a Controller as a WebService and It works fine in a previous work. So, I tested with this

function test(){
   $m = $this->input->post('key');
   echo $m;     
}

and I'm sending a POST JSON data as follow:

{
'key':'hello!"'
}

and now I receive nothing. Some else?