DWF
DWF

Reputation: 360

PHP and Google's reCaptcha v2 - empty response every time

We have a contact.html form that uses reCaptcha v2, whose backend processing is in a php file.

I've taken enough steps to understand that when we send the verification to google's api, the response comes back empty. Below is code that gave me this proof.

$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST["g-recaptcha-response"].'&remoteip='.$_SERVER['REMOTE_ADDR'];
    $verify = file_get_contents($url);
    echo $url;
    if (empty($verify)) echo 'Failed to fetch data';`

However, when I manually enter the url into a browser, I get a JSON response back that indicates success.

What, then, is the difference? Why would file_get_contents return empty if a simple get request from a Chrome browser give me trouble?

I have read that file_get_contents is synchronous, so I wouldn't expect this is just a noob error on waiting for the response.

Any help would be appreciated, this is my very first time working with PHP. It's not hard, but I may be missing something vital.

Upvotes: 1

Views: 1826

Answers (2)

Marcio Vitor
Marcio Vitor

Reputation: 11

Change the configuration in php.ini file and don't need curl.

allow_url_fopen=0 to allow_url_fopen=1

Upvotes: 1

DWF
DWF

Reputation: 360

Sorry everyone, I can't understand why, but the problem was in the method used to access the site verify.

Using curl syntax, I finally got it working.

Upvotes: 3

Related Questions