0

I'm trying to implement reCAPTCHA in my website, everything seems working fine, except the return from file_get_contents().

Here is my code:

if ($_REQUEST["send"] == 1){

    // access
    $secretKey = 'my_key';
    $captcha = $_POST['g-recaptcha-response'];
    $ip = $_SERVER['REMOTE_ADDR'];

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

    echo ($responseKeys);exit;

    if(intval($responseKeys["success"]) !== 1) {
        $message = 'Invalid reCAPTCHA';
    } else {

        $msg = 'content';

        send_mail('send_to',"Subject",$msg);
        header("location:index.php?send=1");exit;

    }

} 

My variable response is returning empty.

I tried to open https://www.google.com/recaptcha/api/siteverify? inserting manually the variables and it seems to work fine.

Am I forgeting something?

Thanks

Marcos Felipe
  • 100
  • 13
  • 1
    You should url encode your parameters. And are the `fopen wrappers` enabled (see the php manual: http://php.net/manual/en/function.file-get-contents.php)? – jeroen Dec 21 '18 at 10:58
  • 1
    I think to use file_get_contents with external url you must configure allow_url_fopen to true in your server. I don't know if thats your problem. – Curlas Dec 21 '18 at 10:58
  • 1
    Possible duplicate of [How to get file\_get\_contents() to work with HTTPS?](https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https) – Eriks Klotins Dec 21 '18 at 11:01
  • 1
    Check your PHP error log to see if you're getting an error from `file_get_contents()`. – Barmar Dec 21 '18 at 11:05
  • Just FYI, it should be done with a POST request. See https://developers.google.com/recaptcha/docs/verify – frobinsonj Dec 21 '18 at 11:10
  • My code is returning a warn: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0, but I don't have access to change my php.ini file to include "allow_url_fopen". Is there another way to do that? – Marcos Felipe Dec 21 '18 at 11:17

2 Answers2

0

Their API waiting for a POST request. Your code send GET request.

See answer here How to post data in PHP using file_get_contents?

Maxim
  • 2,233
  • 6
  • 16
  • Thanks for your answer. I tried to input manually the URL with GET and it seems to be returning ok: { "success": true, "challenge_ts": "2018-12-21T10:41:36Z", "hostname": "www.xxxxxxx.com" } – Marcos Felipe Dec 21 '18 at 11:21
  • did you try to use $_SERVER['HTTP_CLIENT_IP'] instead of $_SERVER['REMOTE_ADDR'] – Maxim Dec 21 '18 at 11:28
0

My wrappers were disabled, reason why I couldn't reach the URL and get the return.

As I don't have access to php.ini the workaround was send the request with curl, here is the code:

    $url = "https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip;
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        echo curl_error($ch);
        echo "\n<br />";
        $response = '';
    } else {
        curl_close($ch);
    }

    if (!is_string($response) || !strlen($response)) {
        echo "Failed to get contents.";
        $contents = '';
    }

    $responseKeys = json_decode($response,true);
Marcos Felipe
  • 100
  • 13