ariefbruce ariefbruce - 2 months ago 14
PHP Question

How to get value success and value true recaptcha and place to if condition?

this is the code,

$response=@file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);


and this the result when I print_r($response);

{ "success": true, "challenge_ts": "2016-09-17T03:11:53Z", "hostname": "www.blabla.com" }


actually I want to make codition with if condition.
for example :

if ($response success == true)


how do that?

Answer

The Response you are getting is in JSON format so for that you have to decode to php array and after that you can use it, see below:

$response=@file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);

$data = json_decode($response, true);
if($data['success'] == true) {
    // Here your code
}
Comments