Öncelikle doğrulaması yapacağımız API'yle iletişim için ben cURL-Easy adında bir wrapper kullandım. --Tavsiye ederim.-- Laravel'e eklemek için aşağıdaki packeti composer.json dosyanızda tanımlayıp "composer update" komutunu CLI'dan çalıştırın:
This file contains 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
"stil/curl-easy": "dev-master", |
./app/filters.php dosyasında uygulamanın başlatıldığı zamanı temsil eden App:before filtresinde Validator'ü genişleterek "recaptcha" adında bir rulea sahip oluyoruz.
This file contains 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 | |
Validator::extend('recaptcha', function($attribute, $value, $parameters) | |
{ | |
$postData = [ | |
'privatekey' => 'reCAPTCHA PRIVATE KEYINIZ', | |
'remoteip' => Request::getClientIp(), | |
'challenge' => Input::get('recaptcha_challenge_field'), | |
'response' => Input::get('recaptcha_response_field'), | |
]; | |
$request = new \cURL\Request('http://www.google.com/recaptcha/api/verify'); | |
$request->getOptions() | |
->set(CURLOPT_TIMEOUT, 5) | |
->set(CURLOPT_RETURNTRANSFER, true) | |
->set(CURLOPT_POST, count($postData)) | |
->set(CURLOPT_POSTFIELDS, http_build_query($postData)); | |
$response = $request->send(); | |
$content = $response->getContent(); | |
if('true' == substr(trim($content), 0, 4)) | |
return true; | |
return false; | |
}); |
Bu işlemden sonra ./app/en/validation.php'de uygun bir yerde hata mesajının tanımlanması gerekli:
This file contains 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 | |
"recaptcha" => 'The :attribute field is not correct.', |
No comments:
Post a Comment