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;
}