【问题标题】:reCaptcha file_get_contents(): SSL operation failedreCaptcha file_get_contents():SSL 操作失败
【发布时间】:2015-11-21 23:31:03
【问题描述】:

我正在为我的网页使用 Google reCaptcha。

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

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

警告:file_get_contents():SSL 操作失败并出现代码 1. OpenSSL错误信息:error:14090086:SSLroutines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /vendor/google/recaptcha/src/ReCaptcha/RequestMethod/Post.php68

警告:file_get_contents(): 无法启用加密 /vendor/google/recaptcha/src/ReCaptcha/RequestMethod/Post.php68

警告行: 文件获取内容(https://www.google.com/recaptcha/api/siteverify): 无法打开流:操作失败 /vendor/google/recaptcha/src/ReCaptcha/RequestMethod/Post.php 在线 68
["invalid-json"]

我这样调用 reCaptcha API:

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

如谷歌开发者页面所述。

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

我希望有人可以帮助我。

【问题讨论】:

  • 您是否通过 https 调用了 google javascript 文件?直播主机是否允许使用file_get_contents 获取网址?
  • @RamRaider 第二个问题我不知道,得问hoststar
  • 尝试使用file_get_contents( 'http://example.com/' ),看看测试页面上会发生什么 - 在实时站点上。如果您收到响应,则主机允许使用 fopen 包装器,否则您可能需要重新考虑...
  • @RamRaider 如果我发出 http 请求,它可以工作,如果我发出 https 请求,它就不起作用......我无法更改 reCaptcha API

标签: php ssl file-get-contents recaptcha


【解决方案1】:

在回复您的最后一条评论时,我意识到您无法更改 Google 的 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 的路径可以使用任一 - 将副本保存到您的主机并更正路径以适合您的环境。

【讨论】:

  • 将数组名从 http 更改为 ssl 并在本地添加证书使其工作,谢谢 .. 看看我的下一个关于通过 POST 请求传递参数的问题...stackoverflow.com/questions/33856354/…跨度>
【解决方案2】:

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 是前端通过 post 发送的 recaptcha 响应。

【讨论】:

    猜你喜欢
    • 2015-04-27
    • 2021-05-14
    • 2022-08-03
    • 2016-12-19
    • 2014-11-26
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多