Search code examples
phpcodeignitererror-handlingrecaptcha

MISSING-INPUT-RESPONSE recaptcha error in codeigniter php


i have created recaptcha for my form in codeigniter, the recaptcha function is like below in my controller:

public function validate_captcha() {
          $recaptcha = trim($this->input->post('g-recaptcha-response'));
          $userIp= $this->input->ip_address();
          $secret='6LcuEP4UAAAAAGa1zwXxGTV0r1fNHMZqnTGeN-c_';
          
          $secretdata = array(
              'secret' => "$secret",
              'response' => "$recaptcha",
              'remoteip' =>"$userIp"
          );

          $verify = curl_init();
          curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
          curl_setopt($verify, CURLOPT_POST, true);
          curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($secretdata));
          curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
          $response = curl_exec($verify);
          $status= json_decode($response, true);
          
          if(empty($status['success'])){
              return FALSE;
          }else{
              return TRUE;
          }
      }

now the problem is this is giving me the following error:

{"SUCCESS":FALSE,"ERROR-CODES":["MISSING-INPUT-RESPONSE"]}

can anyone please tell me what i did wrong in my code, thanks in advance


Solution

  • I think Method should be GET instead of POST.

    Please use below code to verify Google captcha.

    <?php 
    
    public function validate_captcha() {
      $recaptcha = trim($this->input->post('g-recaptcha-response'));
      $userIp= $this->input->ip_address();
      $secret='6LcuEP4UAAAAAGa1zwXxGTV0r1fNHMZqnTGeN-c_';
    
      $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$recaptcha.'&remoteip='.$userIp);
      $responseData = json_decode($verifyResponse);
        if(!$responseData->success){
            echo "failed";
        }else{
            echo "success";
        }
    
    }