Vitas
Vitas

Reputation: 50

reCAPTCHA v3 score not returning

I am trying to implement Google reCaptcha to my website, it all works, but when trying to output score from response it always return NULL

    if(!empty($this->request->data) && !empty($this->request->data['reToken'])){
        $secretKey = "Key";
        $response = file_get_contents(
            "https://www.google.com/recaptcha/api/siteverify?secret=" . $secretKey . "&response=" . $this->request->data["reToken"] . "&remoteip=" . $_SERVER['REMOTE_ADDR']
        );
        if($this->request->data['username'] == "email"){
            var_export($response);
            var_export($response->score);
            exit();
        }
    }

Output

'{ "success": true, "challenge_ts": "2020-08-19T07:27:25Z", "hostname": "hostname", "score": 0.9, "action": "actionName" }' NULL

Upvotes: 0

Views: 1120

Answers (1)

SwissLoop
SwissLoop

Reputation: 499

You try to access a property of a string. $response is a JSON string. So you must decode it before using it.

See json-decode PHP doc

<?php    
    ....
    $decoded = json_decode($response);
    var_export($decoded->score);
    ....
?>

Upvotes: 1

Related Questions