Skip to content

Instantly share code, notes, and snippets.

@memandip
Last active August 18, 2020 10:44
Show Gist options
  • Select an option

  • Save memandip/20ae08d814b58c921cd65877346b0eae to your computer and use it in GitHub Desktop.

Select an option

Save memandip/20ae08d814b58c921cd65877346b0eae to your computer and use it in GitHub Desktop.
Google recaptcha v2 validation server side implementation in PHP
public function validate($response = "captcha token generated from client side")
{
$url = "https://www.google.com/recaptcha/api/siteverify";
$data = [
'secret' => 'google_site_secret',
'response' => $response
];
$options = [
'http' => [
'header' => 'Content-Type: application/x-www-form-urlencoded\r\n',
'method' => 'POST',
'content' => http_build_query($data)
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$response = json_decode($response, true);
return !!$response['success'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment