0

所以我能够让我的 php 联系表正常工作,但现在我想将它与 hCaptcha 集成。我不断遇到的问题是,即使您正确选择了正确的图像,响应也不成功。查看他们的文档(hCaptcha-docs),我看到他们的令牌是从$_POST['h-captcha-response']. 从我看到的示例中,他们使用了一个包含 API 密钥和响应的数组,但我不确定我是否设置正确。我还假设调用 api 的最佳方法是使用 cURL 但不确定,因为我已经看到示例使用file_get_contents()

下面是我的代码,我尝试通过执行var_dum错误来进行调试。它随机返回了一个长长的刺痛,string(2168)

<?php
  if (isset($_POST['submit'])) {
    if (isset($_POST['h-captcha-response']) && !empty($_POST['h-captcha-response'])) {
      // verify response
      $data = array(
        'secret' => "SECRET-KEY",
        'response' => $_POST['h-captcha-response']
        );
      $verify = curl_init();
      $url = "https://hcaptcha.com/siteverify";

      curl_setopt($verify, CURLOPT_URL,$url);
      curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
      curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);

      $verifyResponse = curl_exec($verify);
      $responseData = json_decode($verifyResponse);

      $name = !empty($_POST['name'])?$_POST['name']:'';
      $email = !empty($_POST['email'])?$_POST['email']:'';
      $message = !empty($_POST['message'])?$_POST['message']:'';

      if ($response->success) {
        // contact form submission
        $toEmail = 'support@example.com';
                $subject = 'Contact Request From '.$name;
                $body = '<h2>Contact Request</h2>
                    <h4>Name</h4><p>'.$name.'</p>
                    <h4>Email</h4><p>'.$email.'</p>
                    <h4>Message</h4><p>'.$message.'</p>
                ';

                // Email Headers
                $headers = "MIME-Version: 1.0" ."\r\n";
                $headers .="Content-Type:text/html;charset=UTF-8" . "\r\n";

                // Additional Headers
                $headers .= "From: " .$name. "<".$email.">". "\r\n";
        if(mail($toEmail, $subject, $body, $headers)){
                    // Email Sent
                    $msg = 'Your email has been sent';
                    $msgClass = 'alert-success';
                } else {
          // Failed Email
          $msg = 'Your email could not be sent';
                    $msgClass = 'alert-success';
        }
      } else {
        // Failed hCaptcha validtion
        $debugC = print_r($_POST['h-captcha-response']);
        $msg = 'hCaptcha verification failed. Please try again'."\r\n".$debugC;
        $msgClass = 'alert-danger';


      }
    } else {
      // hCaptcha is empty and was not sellected
      $msg = 'Please click on the hCaptcha button before submitting';
      $msgClass = 'alert-danger';
    }
  }
 ?>


 <!Doctype html>
 <html lang="en" id="switch">

 <head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <link rel="stylesheet" href="css/nexus.css">

   <title>hCaptcha On PHP Contact Form</title>

   <link rel="icon" href="symbol.png" type="image/x-icon" />
   <script src="https://js.hcaptcha.com/1/api.js" async defer></script>
 </head>

 <body>
   <div class="container" style="padding-top: 100px;padding-bottom: 40px;">
        <?php if($msg != ''): ?>
            <div class="alert <?php echo $msgClass; ?>"><?php echo $msg; ?></div>
        <?php endif; ?>
       <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                <h3>Send us a message!</h3>
          <div class="form-group">
              <label>Name</label>
              <input type="text" name="name" class="form-control" value="<?php echo isset($_POST['name']) ? $name : ''; ?>">
          </div>
          <div class="form-group">
            <label>Email</label>
            <input type="text" name="email" class="form-control" value="<?php echo isset($_POST['email']) ? $email : ''; ?>">
          </div>
          <div class="form-group">
            <label>Message</label>
            <textarea name="message" class="form-control"><?php echo isset($_POST['message']) ? $message : ''; ?></textarea>
          </div>
         <div class="form-group">
           <div class="h-captcha" data-sitekey="SITEKEY"></div>
         </div>
          <br>
          <button type="submit" name="submit" class="btn btn-primary">Submit</button>
       </form>
     </div>


   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
   <script src="js/bootstrap.min.js"></script>
   <script src="js/sh.js"></script>

 </body>

 </html>
4

0 回答 0