dsoy71058 2019-01-18 07:55
浏览 120
已采纳

Google Invisible reCaptcha成功后,将多个表单字段数据发布到第三方服务器?

So I am new to this so please excuse me if my question is not posted as expected. Any suggestions and advice will be kindly appreciated.

So I have a form with multiple fields that posts to an PHP file that verifies an Invisible Google reCAPTCHA and then proceeds to post to Pardot (third party software that notifies our sales team)

Following this post How to Post Form Data to 3rd Party Server After Google Invisible reCaptcha Success? I can successfully send the email form field to Pardot but I can not seem to send any other fields and/or replace the email field with another one. In other words I have two fields name="firstname" and name="email" when I send the email field it posts but if I change "email" to "firstname" in the PHP it does not fire.

Based on what I have read I am relatively sure I will need to create an array on the CURLOPT_POSTFIELDS section of my PHP that currently only sends one value ($pardotPost) but before I attempt to send an array I wanted to test the other form fields to see if works as mentioned above.

Here is my client side markup:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Form</title>

    <style>
      input:required:invalid, input:focus:invalid {
        /* insert your own styles for invalid form input */
        -moz-box-shadow: none;
        color: red!important;
      }

      input:required:valid {
        /* insert your own styles for valid form input */
        color: green!important;
      }
    </style>

    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <script>
      function onSubmit(token) {
        document.getElementById("pardot-form-full-width").submit();
      }
    </script>
</head>
<body>

  <div class="mn-call-form-wrapper">

    <form id="pardot-form-full-width"
          class="uk-form uk-grid-medium uk-form-horizontal invisible-recaptcha"
          action="reCAPTCHA.php"
          method="POST"
          enctype="multipart/form-data"
          uk-grid>

      <!-- First Name -->
      <div class="uk-width-1-2@s">
        <input placeholder="First Name *"
               class="mix-contact-form-item uk-input"
               type="text"
               id="firstname"
               name="firstname"
               required=”required”/>
      </div>
      <!-- END - First Name -->

      <!-- Email Address -->
      <div class="uk-width-1-2@s">
        <input placeholder="Email *"
               class="mix-contact-form-item uk-input"
               type="email"
               id="email"
               name="email"
               required="required"/>
      </div>
      <!-- END - Email Address -->

      <!-- Submit Button -->
      <div class="mix-signup-submit-button-wrapper">
        <button class="g-recaptcha"
                data-sitekey="myGrecaptchaKeyIsHere"
                data-callback="onSubmit"> Send <span uk-icon="arrow-right" class="uk-icon"></span>
        </button>
      </div>
      <!-- END - Submit Button -->

    </form>
  </div>
</body>
</html>

Here is my server side markup (reCAPTCHA.php):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Results</title>
</head>
<body>

    <?php
        // reCaptcha info
        $secret = "mySecretKey";
        $remoteip = $_SERVER["REMOTE_ADDR"];
        $url = "https://www.google.com/recaptcha/api/siteverify";

        // Form info
        $firstname = $_POST["firstname"];
        $response = $_POST["g-recaptcha-response"];

        // Curl Request
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, array(
            'secret' => $secret,
            'response' => $response,
            'remoteip' => $remoteip
            ));
        $curlData = curl_exec($curl);
        curl_close($curl);

        // Parse data
        $recaptcha = json_decode($curlData, true);

        if ($recaptcha["success"]) {
            echo "Thank you, we will be in contact with you soon.";

            $pardotPost ='firstname='. $_POST["firstname"];
            $curl_handle = curl_init();
            $url = "http://pardot.com/our/url";
            curl_setopt ($curl_handle, CURLOPT_URL,$url);
            curl_setopt($curl_handle, CURLOPT_POST, true);
            curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $pardotPost);
            curl_setopt( $curl_handle, CURLOPT_SSL_VERIFYPEER, false );
            $result = curl_exec ($curl_handle);
            curl_close ($curl_handle);
        }

        else {
            echo "Oh no, it seems something went wrong.";
        }
    ?>
</body>
</html>

In the PHP sections below if I change the values from firstname to email I can confirm the data is sent and ingested by Pardot

// Does not work
$firstname = $_POST["firstname"];
$pardotPost ='firstname='. $_POST["firstname"];

// Does work
$email = $_POST["email"];
$pardotPost ='email='. $_POST["email"];

So my question is two parts. One - why does the form submit if the email value is used and secondly how would I go about adding several other form fields and send them to Pardot after successful (invisible) Google reCAPTCHA validation?

Thanks in advance!

</div>
  • 写回答

1条回答 默认 最新

  • dtz8044 2019-01-18 12:36
    关注

    Okay so this seems to work:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Results</title>
    </head>
    <body>
    
        <?php
            // reCaptcha info
            $secret = "key-goes-here";
            $remoteip = $_SERVER["REMOTE_ADDR"];
            $url = "https://www.google.com/recaptcha/api/siteverify";
    
            // Form info
            $email = $_POST["email"];
            $firstname = $_POST["firstname"];
            $lastname = $_POST["lastname"];
            $phone = $_POST["phone"];
            $querytype = $_POST["querytype"];
            $message = $_POST["message"];
            $termsconditionsfw = $_POST["termsconditionsfw"];
            $response = $_POST["g-recaptcha-response"];
    
            // Curl Request
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, array(
                'secret' => $secret,
                'response' => $response,
                'remoteip' => $remoteip
                ));
            $curlData = curl_exec($curl);
            curl_close($curl);
    
            // Parse data
            $recaptcha = json_decode($curlData, true);
    
            if ($recaptcha["success"]) {
                echo "Thank you, we will be in contact with you soon.";
    
                //extract data from the post
                //set POST variables
                $url = 'http://explore.mixtelematics.com/l/69882/2019-01-15/d3zr3d';
                $fields = array(
                  'email' => urlencode($_POST['email']),
                    'firstname' => urlencode($_POST['firstname']),
                    'lastname' => urlencode($_POST['lastname']),
                    'phone' => urlencode($_POST['phone']),
                    'querytype' => urlencode($_POST['querytype']),
                    'message' => urlencode($_POST['message']),
                    'termsconditionsfw' => urlencode($_POST['termsconditionsfw']),
                );
    
                //url-ify the data for the POST
                foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
                rtrim($fields_string, '&');
    
                //open connection
                $ch = curl_init();
    
                //set the url, number of POST vars, POST data
                curl_setopt($ch,CURLOPT_URL, $url);
                curl_setopt($ch,CURLOPT_POST, count($fields));
                curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    
                //execute post
                $result = curl_exec($ch);
    
                //close connection
                curl_close($ch);
            }
    
            else {
                echo "Oh no, it seems something went wrong.";
            }
        ?>
    </body>
    </html>

    When I submit this, it sends the information to Pardot :)

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥30 itest不允许查看成绩怎么办
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏