谷歌的reCAPTCHA验证

时间:2014-12-05 14:07:09

标签: php validation laravel response recaptcha

我是网络开发(或开发)的新手,我正在使用Laravel(如果它以某种方式重要)。我的表格中有一个reCAPTCHA。它有效,但现在我必须在recaptcha中验证用户的关键字。

文档:https://developers.google.com/recaptcha/docs/verify听起来很简单,但不知怎的,我无法编写代码行进行验证。我正在寻找一些例子,但它们不适用于Laravel,而是适用于纯PHP,我没有看到示例中使用的“https://www.google.com/recaptcha/api/siteverify?secret=your_secret&response=response_string&remoteip=user_ip_address”URL在哪里。

我的控制器是:

public function postMessage() {

    require_once('recaptchalib.php');
    $privatekey = "MY_PRIVATE_KEY";

    ?????

    // this should be executed only after validation 
    DB::table('messages')->insert(['name' => Input::get('name'),
                                   'email' => Input::get('email'),
                                   'message' => Input::get('message'),
                                   'datetime' => \Carbon\Carbon::now()->toDateTimeString()]);

所以,请告诉我,我怎么能得到JSON对象的响应,或者我应该写什么而不是?????

1 个答案:

答案 0 :(得分:1)

嗯,最后我能够自己找到解决方案:

public function postMessage() {

    $secret   = '7LdL3_4SBAAAAKe5iE-RWUya-nD6ZT7_NnsjjOzc'; 
    $response = Input::get('g-recaptcha-response');
    $url      = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$response;
    $jsonObj  = file_get_contents($url);
    $json     = json_decode($jsonObj, true);

    if ($json['success']==true) {

        DB::table('messages')->insert(['name' => Input::get('name'),
                                       'email' => Input::get('email'),
                                       'message' => Input::get('message'),
                                       'datetime' => \Carbon\Carbon::now()->toDateTimeString()]);

        return Redirect::route('contact')->with('okmessage', 'Ďakujeme. Vaša správa bola odoslaná');

    } else {


        return Redirect::route('contact')->with('errormessage', 'Chyba! Zaškrtnite "Nie som robot" a zadajte správny kód.');
    }       

}