PHP - Google Recaptcha - file_get_contents():SSL操作失败,代码为1

时间:2018-04-05 14:47:32

标签: php openssl recaptcha

我在PHP应用程序中使用Google ReCaptcha库。它已经可靠地工作了很长一段时间。但是,今天,我开始收到与库有关的错误。

public function submit(RequestParameters $params)
{
    /**
     * PHP 5.6.0 changed the way you specify the peer name for SSL context options.
     * Using "CN_name" will still work, but it will raise deprecated errors.
     */
    $peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
    $options = array(
        'http' => array(
            'header' => "Content-type: application/x-www-form-urlencoded\r\n",
            'method' => 'POST',
            'content' => $params->toQueryString(),
            // Force the peer to validate (not needed in 5.6.0+, but still works)
            'verify_peer' => true,
            // Force the peer validation to use www.google.com
            $peer_key => 'www.google.com',
        ),
    );
    $context = stream_context_create($options);
    return file_get_contents(self::SITE_VERIFY_URL, false, $context);
}

我没有对我的申请进行任何更改。这个问题突然开始(从我的角度来看)没有合理的解释。

供参考,以下是Google图书馆的Post.php(不是由我撰写的)。

OpenSSL 1.0.2k 26 Jan 2017

最后一行是&#34; 68&#34;。我使用的是PHP 7.1。与 // validate ReCaptcha $response = null; $reCaptcha = new \ReCaptcha\ReCaptcha(RECAPTCHA_SECRET); if ($_POST["g-recaptcha-response"]) { $response = $reCaptcha->verify( $_POST["g-recaptcha-response"], $_SERVER["REMOTE_ADDR"] ); } 。我按如下方式调用库:

ul li { padding-left: 100px; }

任何建议都将不胜感激。该应用程序托管在IIS和Windows Server上。

4 个答案:

答案 0 :(得分:2)

Mikail G.的答案几乎是正确的,你需要通过CURL访问它。我认为有些事情已被改变,实际上阻止了你当前(和我的)工作,因为我从最近几天看到了几篇关于它的帖子。

请改用:

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'secret' => $secretKey,
        'response' => $captcha,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    ],
    CURLOPT_RETURNTRANSFER => true
]);

$output = curl_exec($ch);
curl_close($ch);

$json = json_decode($output);

答案 1 :(得分:1)

    <?php
    if(isset($_POST['submit']))
    {
    $stream_opts = [
    "ssl" => [
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ]
    ];  
$secret = '6Le__FoUAAXXXXXXXXXXXXXXoQtXhJfdZi92ZPHaAj';
$gRecaptchaResponse = $_POST['g-recaptcha-response']; 
$remoteIp = $_SERVER['REMOTE_ADDR']; 
$url="https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$gRecaptchaResponse&remoteip=$remoteIp";
    $response=file_get_contents($url,false, stream_context_create($stream_opts));
    $result = json_decode($response);
    if ($result->success) 
    {

        header("location: index.php");
    }
    else
    echo 'Captcha verification failed.
}
?>

无需包含autoload.php文件。只需在关闭标记之前包含下面的文件 <script src='https://www.google.com/recaptcha/api.js'></script>在提交按钮之前添加以下代码<div class="g-recaptcha" data-sitekey="6Le__FoUAAXXXXXXXXXXXXXXoQtXhJfdZi92ZPHaAj"></div>

将recaptcha v2的所有文件夹复制并粘贴到您网站的主目录中。这将100%在没有ssl的localhost下工作。感谢

答案 2 :(得分:0)

为了解决这个问题,您需要使用“http”调用google api,或使用不同的方式发出请求,例如curl,这里的功能是:

function file_get_contents_curl($url, $data (array)) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        json_encode($data));



    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec ($ch);

    curl_close ($ch);



    return $server_output;
}

答案 3 :(得分:-1)

相关问题