Tomas Turan
Tomas Turan

Reputation: 1255

Google's reCAPTCHA verification

I'm a newbie in web development (or development) and I'm using Laravel (if it matters somehow). I have a reCAPTCHA in my form. It works, but now I have to verify user's keyword in recaptcha.

Documentation: https://developers.google.com/recaptcha/docs/verify sounds simple, but somehow I can't write the code lines for verification. I was looking for some examples, but they're not for Laravel but for pure PHP, and I didn't see where is the 'https://www.google.com/recaptcha/api/siteverify?secret=your_secret&response=response_string&remoteip=user_ip_address' URL used in the examples.

My controller is:

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()]);

So please, instruct me, how could i get JSON object with response, or what should I write instead of ?????

Upvotes: 0

Views: 689

Answers (1)

Tomas Turan
Tomas Turan

Reputation: 1255

Well, at the end I was able to find solution by myself:

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

}

Upvotes: 1

Related Questions