Created
May 26, 2021 22:31
-
-
Save halityurttas/19b10d9287f01243d3e330cc9137caa1 to your computer and use it in GitHub Desktop.
ReCaptcha v2 Laravel Validation Rule
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 Illuminate\Contracts\Validation\Rule; | |
class recaptcha2 implements Rule | |
{ | |
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode('recaptcha-secret-key') . '&response=' . urlencode($value); | |
$response = file_get_contents($url); | |
$responseKeys = json_decode($response,true); | |
return $responseKeys["success"]; | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return 'recaptcha-not-valid'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment