Last active
September 2, 2017 15:21
-
-
Save devsrv/713aee4e25c4f8b693bf1ba778693461 to your computer and use it in GitHub Desktop.
Google Recaptcha Verification [Laraval / PHP]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*google recaptcha verification | |
* | |
*@param client ip address | |
*@param recaptcha input data //for laravel-> $request->input('g-recaptcha-response'); or $_POST['g-recaptcha-response']; | |
* | |
*@return bool | |
*/ | |
public function RecaptchaValid($client_ip='127.0.0.1', $response) | |
{ | |
$url = 'https://www.google.com/recaptcha/api/siteverify'; | |
$data = array('secret' => config('services.recaptcha.secret'), 'response' => $response, 'remoteip' => $client_ip); | |
// use key 'http' even if you send the request to https://... | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/x-www-form-urlencoded\r\n", | |
'method' => 'POST', | |
'content' => http_build_query($data) | |
) | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
if ($result === false) | |
{ | |
return false; | |
} | |
else | |
{ | |
$google_res = json_decode($result, true); | |
if($google_res['success'] === true) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment