Skip to content

Instantly share code, notes, and snippets.

@halityurttas
Created May 26, 2021 22:31
Show Gist options
  • Save halityurttas/19b10d9287f01243d3e330cc9137caa1 to your computer and use it in GitHub Desktop.
Save halityurttas/19b10d9287f01243d3e330cc9137caa1 to your computer and use it in GitHub Desktop.
ReCaptcha v2 Laravel Validation Rule
<?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