Response in reCAPTCHA comes back as a none object. PHP Symfony

Response in reCAPTCHA comes back as a none object. PHP Symfony
0

#1

I am stuck here guys I am using symfony 3.4 and I am making an api call to googles reCAPTCHA and when I decode my json response and return a value that is suppose to come back from googles api I get an error stating that it isnt a object, my code in my controller is here. I blocked out my secret for obvious reasons.

/**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request) {

    try {
        $formModel = new Contact();
    } catch (\Exception $exception) {
        throw new \RuntimeException('an error occurred');
    }
    $form = $this->createForm(ContactFormType::class, $formModel);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid() && $this->captchaverify($request->get('g-recaptcha-response'))) {
        $em      = $this->getDoctrine()->getManager();
        $contact = $form->getData();
        $em->persist($contact);
        $em->flush();

        return new JsonResponse([
            'status' => 'ok',
        ]);
    } elseif ($form->isSubmitted()) {
        return new JsonResponse([
            'status'   => 'error',
            'errors'   => Utility::getErrorsFromForm($form),
        ]);
    }
    if($form->isSubmitted() &&  $form->isValid() && !$this->captchaverify($request->get('g-recaptcha-response'))){

            $this->addFlash(
                'error',
                'Captcha Require'
              );
            }

        return $this->render('@App/form.html.twig', [
            'form' => $form->createView(),
        ]);
}

function captchaverify($recaptcha){
     $url = "https://www.google.com/recaptcha/api/siteverify";
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, array(
         "secret"=>"xxxxxxxxxx","response"=>$recaptcha));
     $response = curl_exec($ch);
     curl_close($ch);
     $data = json_decode($response);

 return $data->success;
 }

#2

If you are facing that kind of issue to not getting the response the reCaptcha that you can set by the process of the settings easily from some of the few techniques which you can check on the Epson printer recovery mode for that reason that kind of issue can also be solved.


#3

Please do not change your Category back to General. You are seeking help with PHP, so the appropriate category is Help.


#4

In most Web forms, you usually have two files: the form itself with the fields and the file with the script to process the inputs to the form. For more visit HP Support Number


#5

Hey, This is a great article. I was searching for the same and I found the best content here. Thanks for sharing the info. keep on doing the great work.
Sage Live support