reCAPTCHA failed file_get_contents

Asked

Viewed 971 times

2

I am trying to increment a reCAPTCHA on a website, I have watched several step by step, but mine persists in giving the same error. code example:

index.php
<html xmlns="http://www.w3.org/1999/html">
    <head>
        <title>index</title>
        <script src='https://www.google.com/recaptcha/api.js'></script>
    </head>
    <body>
         <form method="post" action="verify.php">
             <input type="text" name="inp"/></br>
             <div class="g-recaptcha" data-sitekey="********************"></div>
             <input type="submit"></br>
         </form>
    </body>
</html>

Verify.php

<?php
header("Content-type: text/php charset=utf-8");
if(isset($_POST['g-recaptcha-response'])&& $_POST['g-recaptcha-response']){
     var_dump($_POST);
     //informações sobre o reCAPTCHA
     $secret = "**************************************************";
     var_dump($secret);
     $ip = $_SERVER['REMOTE_ADDR'];
     var_dump($_SERVER);
     $captcha =  $_POST['g-recaptcha-response'];
     var_dump($captcha);
     //Enviar para o google
     $rps = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip$ip");
     //resposta google
     var_dump($rps);
    // $arr = json_decode($rsp,true)
  }else{
     echo "reCAPTCHA não prenchido";
}

The error always occurs in the

 $rps = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip$ip");

The var_dump($rps); should return "success": true but always returns

Warning: file_get_contents() [Function.file-get-Contents]: URL file-access is disabled in the server Configuration in /home/Storage/2/8a/81/necon1/public_html/Verify.php online 13

  • See in PHP.ini, if you have access, the parameter allow_url_fopen or use CURL as mentioned in the answers.

1 answer

-1

Some servers disable the use of file_get_contents, in other words, google blocked any access from file_get_contents. You can use PHP’s Curl to circumvent the lock. In this case, your code would look like this:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip$ip");
curl_setopt($ch, CURLOPT_HEADER, 0);


$rps = curl_exec($ch);

//print da resposta do google
echo "<pre>"
print_r($rps);

curl_close($ch);

Click here to access the Curl manual.

  • 1

    It wasn’t Google that blocked it, it was itself in PHP.ini, using allow_url_fopen = Off, probably.

  • I have the same error and the allow_url_fopen directive is enabled in php. What you suggest ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.