0

I m using google site verification recaptcha API for my website.

$json = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$response."&remoteip=".$ip);

when I print echo $json; its showing proper response

{ "success": true, "challenge_ts": "2018-08-23T12:43:42Z", "hostname": "staging.my.com" }

but when i tried

$data = json_decode($json,true); echo $data->success;

its showing nothing

Could anybody tell me what i am missing??

Jordan
  • 39
  • 1
  • 13

2 Answers2

1
$json = '{ "success": true, "challenge_ts": "2018-08-23T12:43:42Z", "hostname": "staging.my.com" }';
$data = json_decode($json,true);

This produces an associative array from your example JSON string, not an object (used var_dump($data); to see what you actually have stored). Just use the proper syntax for accessing array values:

echo $data["success"]; // prints '1'

or:

echo ($data["success"])?'success':'failure'; // prints 'success'
Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31
0

According to PHP doc if you set the second parameter assoc to true the function returns associative arrays instead of std class.

 mixed json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )

assoc

When TRUE, returned objects will be converted into associative arrays.

So either try $data['success'] or change json_decode($json, true) to json_decode($json).

mdh.heydari
  • 540
  • 5
  • 13