-3

In my code I want to print a specific value i.e. ticket from JSON data but when I do it I got Notice: Trying to get property of non-object error every time I don't know what I am doing wrong in it

Here is my JSON array dump

array(3) {
  ["status"]=>
  int(200)
  ["msg"]=>
  string(2) "OK"
  ["result"]=>
  array(6) {
    ["ticket"]=>
    string(79) "w-_xdRcVDJQ~55de39b726745a28~1505246565~def~FzuGpPpNoQEus6lK~1~Dhyp8PJM83-pMwAe"
    ["captcha_url"]=>
    string(50) "https://myurl.com/images/FzuGpPpNoQEus6lK.png"
    ["captcha_w"]=>
    int(160)
    ["captcha_h"]=>
    int(70)
    ["wait_time"]=>
    int(0)
    ["valid_until"]=>
    string(19) "2017-09-12 20:17:46"
  }
}

Here is my PHP Code

$response = file_get_contents($url);
$obj = json_decode($response, TRUE);
$printjson = $obj->result->ticket;
echo $printjson;
Rtra
  • 514
  • 12
  • 25
  • 1
    You have __array__, use `[]` notation. – u_mulder Sep 12 '17 at 20:08
  • 4
    do you know what the second argument you are providing to `json_decode()` means ? http://php.net/json_decode – Calimero Sep 12 '17 at 20:08
  • @FirstOne If you think it's a duplicate, you should vote-to-close as such. – Patrick Q Sep 12 '17 at 20:18
  • I already followed these links before posting but it didn't resolve my issue – Rtra Sep 12 '17 at 20:19
  • @PatrickQ Since you assume I don't know that: For some reason, I clicked to check the previous vote-close 'type' and it automatically voted to close (without me even pressing the button). And you probably know this: you can't vote again after retracting... Thanks anyways, I guess – FirstOne Sep 12 '17 at 20:21

1 Answers1

5

json_decode function used with second parameter set to TRUE means that the response will be converted to array not object.

Try using it without the second parameter:

$response = file_get_contents($url);
$obj = json_decode($response);
$printjson = $obj->result->ticket;
echo $printjson;

Reference json_decode.