PHP - Google Recaptcha - file_get_contents(): SSL operation failed with code 1

file_get_contents(): ssl operation failed with code 1. openssl error messages: error:14090086
file_get_contents(): failed to enable crypto
ssl operation failed with code 1. openssl error messages composer
ssl operation failed with code 1 laravel
message: file_get_contents(): failed to enable crypto
file_get_contents(): ssl operation failed with code 1 laravel
file get contents ssl operation failed with code 1
stream_socket_enable_crypto(): ssl operation failed with code 1

I am using the Google ReCaptcha library in my PHP application. It has been working reliably for quite a while now. However, today, I started receiving errors related to the library.

    *[05-Apr-2018 09:19:03 America/Chicago] Severity: 2,Message: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed,File: E:\inetpub\wwwroot\vendor\google\recaptcha\src\ReCaptcha\RequestMethod\Post.php,Line: 68
[05-Apr-2018 09:19:04 America/Chicago] Severity: 2,Message: file_get_contents(): Failed to enable crypto,File: E:\inetpub\wwwroot\vendor\google\recaptcha\src\ReCaptcha\RequestMethod\Post.php,Line: 68
[05-Apr-2018 09:19:04 America/Chicago] Severity: 2,Message: file_get_contents(https://www.google.com/recaptcha/api/siteverify): failed to open stream: operation failed,File: E:\inetpub\wwwroot\vendor\google\recaptcha\src\ReCaptcha\RequestMethod\Post.php,Line: 68*

I did not make any changes to my application. The issue just started suddenly and (from my perspective) without logical explanation.

For reference, here is the Post.php from Google's library (not authored by me).

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);
}

The last line is "68". I am using PHP 7.1. with OpenSSL 1.0.2k 26 Jan 2017. I am calling the library as follows:

    // 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"]
    );
}

Any advice would be greatly appreciated. The application is hosted on IIS and Windows Server.

Mikail G.'s answer is nearly correct, you do need to access it over CURL. I think something has been changed to actually prevent your current (and mine) from working as I have seen several posts about it all from recent days.

Use this instead:

$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);

PHP - Google Recaptcha, PHP - Google Recaptcha - file_get_contents(): SSL operation failed with code 1 · php openssl recaptcha. I am using the Google ReCaptcha  recaptcha not work properly on php 5.6 (php 5.5 work fine) Verify the response string, i got error: Warning: file_get_contents(): SSL operation failed with code 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.
}
?>

No need to include the autoload.php file. Just include the file below just before closing tag <script src='https://www.google.com/recaptcha/api.js'></script> and before the submit button add the following code <div class="g-recaptcha" data-sitekey="6Le__FoUAAXXXXXXXXXXXXXXoQtXhJfdZi92ZPHaAj"></div>

Copy and paste all the folders of recaptcha v2 in home directory of your site. This will 100% work in localhost without ssl. Thanks

file_get_contents(): SSL operation failed with code 1, file_get_contents(): SSL operation failed with code 1 file_get_contents(): Failed to enable crypto in users\includes\recaptcha.config.php on line 86. Warning: file_get_contents(https://www.google.com/recaptcha/api/sit. If you use Magento 2 and get an error like this: Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:error:14095126:SSL routines

In order to fix the issue you need to call the google api with "http" , or use a diffrent way to make request such as curl , here the function to do so :

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;
}

Warning: file_get_contents(): SSL operation failed with code 1 , Warning: file_get_contents(): SSL operation failed with code 1. reading in vendor/google/recaptcha/src/ReCaptcha/RequestMethod/Post.php  PHP 7+ DOMAIN without https, only 80 port google/recaptcha/src/ReCaptcha/RequestMethod/Post.php', 68 I add 'ssl' => array( 'verify_peer' => false, ), Getting started

Why don't you try Step by Step guide to integrate reCpatcha

file_get_contents(): SSL operation failed with code 1. OpenSSL , Please enabled php extension fopen(). And restart the server once. After that it will be work. Happy Coding. :). Google Recaptcha errors Get help with installation and running phpBB 3.2.x here. Please do not post bug reports, feature requests, or extension related questions here.

OpenSSL Error - reCaptcha, and all the /qa/qa-plugin/recaptcha-captcha/recaptchalib.php on line 128. Warning: file_get_contents(): SSL operation failed with code 1. file_get_contents(): SSL operation failed with cod SOLUCIONAR EL PROBLEMA DE ARRANQUE EN UBUNTU Y LIN Obtener el Manual de un Paquete instalado en Ubunt

Error Log, USP: Google reCAPTCHA: invalid-json [16-Dec-2017 15:40:53 UTC] PHP Warning: file_get_contents(): SSL operation failed with code 1. [2017-11-28 19:41 UTC] ab@php.net Thanks for the report. There's not much can be done on the PHP side. The certificate store is updated automatically from some central place, that's why fe. also self signed certs sometimes got deleted besides they're imported manually.

Problem with Google Recaptcha, Seems there's may be a file_get_contents in the Google Recaptcha code that's causing it to fail on my hosting. *27822 FastCGI sent in stderr: "PHP message: PHP Warning: file_get_contents(): SSL: Handshake timed failed to open stream: operation failed in Viewing 4 replies - 1 through 4 (of 4 total). Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Comments
  • I too have all started having this issue as of today... I think it may be something to do with the Symantec debacle
  • Thanks. Though I did not try it out personally, it seems as it might be a good workaround for someone else experiencing the issue. Personally, my resolution actually involved manually downloading the cacert.pem file and then setting the openssl.cafile path in my php.ini file. I am not positive why it suddenly stopped working though.
  • I have had the same issue with it just stopping all of a sudden
  • Thanks for the advice. However, the website that call the reCaptcha library (Google's servers) is not actually accessed over https.
  • Google does not provide https ? , have you tried it ? coz it is not possible that google does not have https option
  • Sorry, but what I meant was my server/website is not accessed over https. However, according to the error logs, it is attempting to access Google over https (specifically google.com/recaptcha/api/siteverify). Also, if it helps, I have an ASP.NET MVC application on the same server that also uses Google's reCaptcha service, and it seems to be working fine.
  • the issue is with SSL certificate , try to call the google api with http request , or try to request the api with curl , coz it seems like when you make the request you can't read their SSL certificate
  • this may help : function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); $data = curl_exec($ch); curl_close($ch); return $data; }
  • This doesn't attempt to answer the question really. Also helpful if you write the answer because this the page linked to could be removed at anytime.
  • @tim.baker - don't worry it won't be removed and if it does for some reason it's always on cache.