· 8 years ago · Jan 27, 2018, 11:58 AM
1<?php
2function post_captcha($user_response) {
3 $fields_string = '';
4 $fields = array(
5 'secret' => '________________SECRET_KEY_______________',
6 'response' => $user_response
7 );
8 foreach($fields as $key=>$value)
9 $fields_string .= $key . '=' . $value . '&';
10 $fields_string = rtrim($fields_string, '&');
11
12 $ch = curl_init();
13 curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
14 curl_setopt($ch, CURLOPT_POST, count($fields));
15 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
16 curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
17
18 $result = curl_exec($ch);
19 curl_close($ch);
20
21 return json_decode($result, true);
22}
23
24// Call the function post_captcha
25$res = post_captcha($_POST['g-recaptcha-response']);
26
27if (!$res['success']) {
28 // What happens when the reCAPTCHA is not properly set up
29 echo 'reCAPTCHA error: Check to make sure your keys match the registered domain and are in the correct locations. You may also want to doublecheck your code for typos or syntax errors.';
30} else {
31 // If CAPTCHA is successful...
32
33 // Paste mail function or whatever else you want to happen here!
34 echo '<br><p>CAPTCHA was completed successfully!</p><br>';
35
36}