0

This code was working 2 days before, but now i am getting an error:

Trying to get property of non-object in C:\xampp\htdocs\ihelploginapi\index.php on line 4.

Somebody please help me out.

<?php

    $json = file_get_contents('php://input');
    $obj = json_decode($json,TRUE);
    $tag = $obj->{'tag'};
?>

3 Answers3

1

json_decode does not give you an object. It gives you an array. You want to access it as such:

$tag = $obj['tag'];

or to re-write the var names more accurately

$json = file_get_contents('php://input');
$php_array = json_decode($json,TRUE);
$tag = $php_array['tag'];
Andrew Coder
  • 1,058
  • 7
  • 13
0

In the related line use :

$tag = $obj['tag'];
pritesh
  • 2,162
  • 18
  • 24
0

The second argument to json_decode() tells it to convert JSON objects to PHP associative arrays, not PHP objects. So you need to use $obj['tag'] instead of $obj->tag. Or change the decode line to

$obj = json_decode($json);
Barmar
  • 741,623
  • 53
  • 500
  • 612