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

时间:2019-01-18 07:55:48

标签: php html forms recaptcha pardot

所以我对此并不陌生,所以如果我的问题没有按预期发布,请原谅。任何建议都将不胜感激。

所以我有一个包含多个字段的表单,该表单发布到一个PHP文件中,该文件验证不可见的Google reCAPTCHA,然后继续发布到Pardot(通知我们销售团队的第三方软件)

关注此帖子How to Post Form Data to 3rd Party Server After Google Invisible reCaptcha Success? 我可以成功将电子邮件表单字段发送到Pardot,但似乎无法发送其他任何字段和/或用另一个字段替换电子邮件字段。 换句话说,当我发送它发布的电子邮件字段时,我有两个字段name =“ firstname”和name =“ email”,但是如果我在PHP中将“ email”更改为“ firstname”,则不会触发。

根据我所读的内容,我相对确定我需要在PHP的CURLOPT_POSTFIELDS部分上创建一个数组,该数组当前仅发送一个值($ pardotPost),但是在尝试发送数组之前,我想测试其他表单字段以查看是否如上所述。

这是我的客户端标记:

<!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>

这是我的服务器端标记(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>

在下面的PHP部分中,如果我将值从firstname更改为email,我可以确认数据是由Pardot发送和提取的

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

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

所以我的问题分为两个部分。 一个-为什么使用电子邮件值时为什么要提交表单,其次我如何添加其他几个表单字段并在成功(不可见)的Google reCAPTCHA验证后将其发送到Pardot?

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,这似乎可行:

<!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>

当我提交此消息时,它将信息发送给Pardot:)

相关问题