file_get_contents()总是返回bool(false),而URL在PHP中的recapctha V2升级期间返回一个值

时间:2018-03-23 06:01:18

标签: php recaptcha

我目前正在更新recaptchalib.php以将验证码从V1升级到V2。 我已成功完成客户端集成。但在服务器端面临问题。在我的下面的代码片段中,如果我直接在浏览器中点击URL,我会得到 "成功" :false,带有"错误代码" as"超时或重复"要么 "成功" :true但在上述两种情况下,file_get_contents()都将值返回为bool(false)。最终,我总是得到错误,因为" reCaptcha没有被正确消化" 。请指出为什么file_get_content()总是返回bool(false)?可以在代码中进行哪些更正,以便我们以正确的格式获得响应,然后成功地执行json_decode,即点击“真实”#39;案件 。 以下是代码段

$postdata = http_build_query(
                 array(
                        'secret'    =>  $privkey,
                        'response' => $response,
                         'remoteip'  =>  $remoteip
                        )
                );
$url = 'https://www.google.com/recaptcha/api/siteverify?'.$postdata;
print "URL is " . $url . "<br>";
$response = file_get_contents($url);

var_dump($response) ;

$responses = json_decode($response, true);

if($responses["success"] === TRUE){
    echo "true";
}else{
    echo "false";
}

1 个答案:

答案 0 :(得分:0)

要使file_get_contents()起作用,传递的URL不得包含任何特殊字符。要获取URL的已清理版本,请使用urlencode()as mentioned here.看起来您的网址可能包含特殊字符(私钥等)。

试试这个:

$url = 'https://www.google.com/recaptcha/api/siteverify?'.$postdata;
$url = urlencode($url);    
$response = file_get_contents($url);