reCaptcha file_get_contents():SSL操作失败


reCaptcha file_get_contents(): SSL operation failed

我的网页使用Google reCaptcha。

在测试模式下,一切正常。没有SSL。

当我在生产环境中测试我的网页时,会出现以下错误:

警告:file_get_contents():SSL操作失败,代码为1.OpenSSL错误消息:错误:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:中的证书验证失败/vendor/google/recaptcha/src/recaptcha/RequestMethod/Post.php68行
警告:file_get_contents():未能在中启用加密/vendor/google/recaptcha/src/recaptcha/RequestMethod/Post.php在线68

警告:文件集内容(https://www.google.com/recaptcha/api/siteverify):无法打开流:中的操作失败/vendor/google/recaptcha/src/recaptcha/RequestMethod/Post.php在线68
["无效json"]

我这样调用reCaptcha API:

<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
                async defer></script>

如谷歌开发者页面上所述。

我正在hoststar.ch上托管我的网页。TSL 1.2正在运行。

我希望有人能帮我。

在回应您的最后一条评论时,我意识到您无法更改谷歌的reCaptcha api——我的意思只是简单地在example.com(它确实存在)上执行file_get_contents作为测试,看看您是否可以使用该方法检索任何内容,因为一些网络主机禁用了相关功能。

但是,对于Google reCatcha API,您可能需要为file_get_contents函数调用指定额外的参数,特别是为SSL专门设置context选项。

$secret = 'Your google secret';
$captcha = trim( $_POST['g-recaptcha-response'] );
$ip = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$captcha}&remoteip={$ip}";
$options=array(
    'ssl'=>array(
        'cafile'            => '/path/to/cacert.pem',
        'verify_peer'       => true,
        'verify_peer_name'  => true,
    ),
);
$context = stream_context_create( $options );
$res=json_decode( file_get_contents( $url, FILE_TEXT, $context ) );
if( $res->success ){/* all good */}
else{ /* captcha failed */ }

如果您还没有cacert.pemca-bundle.crt的副本,您可以从它们各自的链接下载它们。cafile的路径可以使用其中之一-将副本保存到主机,然后根据您的环境更正路径。

file_get_contents更改为curl。这是代码,

更改-

$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
 $captcha_success=json_decode($verify);  /*store json response*/

到此代码:

$ch = curl_init("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$verify = curl_exec($ch);
$captcha_success=json_decode($verify);  /*store json response*/

请注意$secret是存储在服务器端的密钥,$response则是前端通过邮件发送的重述响应。