2014-12-05 136 views
0

我是网络开发(或开发)的新手,我使用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

好了,在最后我能够自己找到解决方法:

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.'); 
    }  

}