Thursday, July 6, 2017

Google no captcha Re captcha integration with laravel angular curl . Response always false

Asked by: Nurul Alam


I am trying to make my contact form work. but so far not working. I have used vcRecaptchaService for angularjs to integrate google recaptcha. Without this i was having problem while routing. That is another issue though. Anyway here is the problem,

I created a laravel controller to handle the form verification

public function postContact(Request $request)
    {   

    $captcha = $request['g-recaptcha-response'];
    $privatekey = "secret key replaced for obvious reason";
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => $privatekey,
        'response' => $captcha
        // 'remoteip' => $_SERVER['REMOTE_ADDR']
    );

    $curlConfig = array(
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $data
    );

    $ch = curl_init();
    curl_setopt_array($ch, $curlConfig);
    $response = curl_exec($ch);
    curl_close($ch);

    //$jsonResponse = json_decode($response);  //if i try to output jsonresponse, it always comes as null.

    if ($response === true) {
       return response()->json([
            'verification' =>'true',
        ]);

    }
    elseif($response === false)
    {
        return response()->json([
            'verification' =>'false',
        ]);   
    }
}

verification always coming as false. What can I do? I am sure I am not doing it properly .Can you see any issue?



Source

No comments:

Post a Comment

Adbox