Created
April 3, 2018 19:14
-
-
Save paulund/de93e817d9652737a84912e1d49cafc7 to your computer and use it in GitHub Desktop.
A Laravel validation rule to verify recaptcha.
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
<?php | |
namespace App\Rules; | |
use GuzzleHttp\Client; | |
use Illuminate\Contracts\Validation\Rule; | |
class GoogleRecaptcha implements Rule | |
{ | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
$client = new Client(); | |
$response = $client->post('https://www.google.com/recaptcha/api/siteverify', | |
[ | |
'form_params' => [ | |
'secret' => env('RECAPTCHA_SECRET_KEY', false), | |
'remoteip' => request()->getClientIp(), | |
'response' => $value | |
] | |
] | |
); | |
$body = json_decode((string)$response->getBody()); | |
return $body->success; | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return 'Are you a robot?'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment