verify recaptcha 3 php

Solutions on MaxInterview for verify recaptcha 3 php by the best coders in the world

showing results for - "verify recaptcha 3 php"
Ella
23 Mar 2019
1private function verify_reCaptcha(){
2		$success = false;
3		try {
4			$url = 'https://www.google.com/recaptcha/api/siteverify';
5			$data = [
6				'secret'   => "secret key",
7				'response' => "token_reCaptcha_from_client",
8				'remoteip' => $_SERVER['REMOTE_ADDR']
9			];
10			
11			$options = [
12				'http' => [
13					'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
14					'method'  => 'POST',
15					'content' => http_build_query($data)
16				]
17			];
18			
19			$context  = stream_context_create($options);
20			$result = file_get_contents($url, false, $context);
21			
22			log_message("error", "reCaptcha ".print_r([$url, $data, $options, $result],true));
23			if($res = json_decode($result)) {
24				if ($res->success){
25					$success = true;
26					$message = "success";
27				}else{
28					$message = "reCaptcha validation failed";
29				}
30			} else {
31				$message = "failed to decode json";
32			}
33		} catch (Exception $e) {
34			$message = $e->getMessage();
35			send_report("reCaptcha Failed : auth.php\n{$message}");
36		}
37		return ["success" => $success, "message" => $message];
38	}