1

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.

DWF
  • 290
  • 2
  • 11
  • Where did you even find this method of recaptcha validation? https://developers.google.com/recaptcha/old/docs/php – Anurag Srivastava Mar 03 '19 at 17:39
  • This was what a previous coder put in there. I used a bit of his code to search for tutorials on it, so I think that was where it came from. Keep in mind, this is reCaptcha v2. Your link is an old document. – DWF Mar 03 '19 at 18:08
  • Link to the new doc is there as well. Why not do it the right way by referring the official docs? – Anurag Srivastava Mar 03 '19 at 18:09
  • https://codeforgeek.com/2014/12/google-recaptcha-tutorial/ is one such link with the recommended method that I was trying to use. – DWF Mar 03 '19 at 18:12
  • There's not really any need for the attitude. I asked for help. If I were the original programmer with a working knowledge of PHP, the purpose of the site, the page, and the workings of every piece of code, I probably would have started from scratch correctly. Yet here we are. Try showing a little grace to people asking for help, please. – DWF Mar 03 '19 at 18:34

2 Answers2

2

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.

DWF
  • 290
  • 2
  • 11
0

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

allow_url_fopen=0 to allow_url_fopen=1

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 30 '22 at 12:59