reCAPTCHA v3 分数未返回

reCAPTCHA v3 score not returning

我正在尝试在我的网站上实施 Google reCaptcha,一切正常,但是当尝试从响应中输出分数时,它总是 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();
        }
    }

输出

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

您尝试访问一个 属性 的字符串。 $response 是一个 JSON 字符串。所以使用前一定要先解码。

json-decode PHP doc

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