My website is a WordPress website. I wanted to create a form with a reCaptcha protection using Google reCaptcha v2 (tick box). Surprisingly it doesn’t work even though it works on my other website using same PHP code.
After Ticking the reCaptcha

It doesn’t work if use file_get_contents
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$data = array('secret' => 'your-secret', 'response' => $_POST['g-recaptcha-response']); $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' .$data['secret'] . '&response=' . $data['response']; $result = file_get_contents($url); $result = json_decode($result); if(!$result->success): echo 'error'; return; endif; echo 'success'; |
Google reCaptcha keeps sending me error.
It works if using curl
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$url = 'https://www.google.com/recaptcha/api/siteverify'; $data = array('secret' => 'your secret key', 'response' => $_POST['g-recaptcha-response']); $verify = curl_init(); curl_setopt($verify, CURLOPT_URL, $url); curl_setopt($verify, CURLOPT_POST, true); curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($verify, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($verify); $responseData = json_decode($response); if($responseData->success): // your success code goes here // echo 'success'; return true; else: // return error to user; they did not pass // echo 'fail'; return false; endif; |