ReCaptcha file_get_contents (): SSL operation error

I am using Google reCaptcha for my webpage.

In test mode, everything works fine. No SSL.

When I test my webpage in production, the following errors occur:

Warning : file_get_contents (): SSL operation failed with code 1. OpenSSL error messages: error: 14090086: SSL procedures: SSL3_GET_SERVER_CERTIFICATE: certificate verification failed <B> / vendor / google / recaptcha / src / ReCaptcha / RequestMethod / Post. phpon line 68

Warning : file_get_contents (): Failed to enable cryptography in <B> /vendor/google/recaptcha/src/ReCaptcha/RequestMethod/Post.phpon line 68

Warning : file_get_contents ( https://www.google.com/recaptcha / api / siteverify ): could not open the stream: the operation failed <B> /vendor/google/recaptcha/src/ReCaptcha/RequestMethod/Post.phpon line 68
["invalid-json"]

I call the reCaptcha API as follows:

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

as described on the developer page from Google.

I host my webpage on hoststar.ch. Works TSL 1.2.

I hope someone can help me.

+3
source share
1 answer

In response to your last comment, I understand that you cannot change Google reCaptcha api- I meant just to file_get_contentsactually do example.com(it exists) as a test to find out if you can get any using this method, since some web hosts disable related functionality.

API reCatcha Google file_get_contents, , context SSL.

$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.pem ca-bundle.crt . cafile - , .

+4

Source: https://habr.com/ru/post/1616957/


All Articles