I use php to program a signin form How can i solve problem with recaoptcha v3

Viewed 19

I've this error on my program: Undefined property: stdClass::$score

This is my code:

protected function signinCaptcha():string|bool
 {
    
    
    //echo "test: -> "; var_dump($this->saveForm());exit;
     if ($this->saveForm()):
        //echo "test: -> "; var_dump($this->saveForm());exit;
         $response = $this->signin['recaptcha_response'];
         $secretKey ='XXXXXXXXXXXXXXXXXXXXXXXXXX';
         
         
         // post request to server
         $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
         $captcha = file_get_contents($recaptcha_url . "?secret=" . $secretKey . "&response=" . $response);
         
         $captcha = json_decode($captcha); 

         if($captcha->score >= 0.5)
             return true;
         else
             return false;
     else:
         return false;
     endif;
}   

Could anyone help me to understand where the cause of the problem lies and how to go about resolving it?

1 Answers

Longtime ago, I created a method in a class to do that, for a personnal use :

public static function reCaptcha($response){
    $postdata = http_build_query(
        array(
            'secret' => 'mysecret',
            'response' => $response,
            "remoteip" => $_SERVER['REMOTE_ADDR'],
        ) 
    );

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-Type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);
    $result = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);

    return json_decode($result);
}

The point : look how to create the file_get_contents stream.

Related