如何在 Google 隐形 reCaptcha 成功后 Post 向第三方服务器形成数据?

How to Post Form Data to 3rd Party Server After Google Invisible reCaptcha Success?

我有一个可能是非常基本的问题,但我对 PHP 和表单创建还很陌生,所以希望有人能帮助我。

我们有垃圾邮件机器人不断地在我们的网站上提交单字段电子邮件表单,尽管有蜜罐方法,所以我希望使用 Google 的 Invisible reCaptcha 来解决这个问题。

我正在按照这个有用的指南中的说明进行操作:https://www.pinnacleinternet.com/installing-invisible-recaptcha/但是我遇到困难的地方是,在结果成功之后,我想使用通过提交的电子邮件地址表单,然后 post 将其发送到第三方服务器(在本例中为我们的营销自动化工具 Pardot)。

这是隐形验证码:

前端

<script>
function captchaSubmit(data) {
document.getElementsByClassName("invisible-recaptcha").submit();
}
</script>  

<form action="utils/recaptcha.php" method="post" class="pardot-email-form-handler invisible-recaptcha" novalidate>    
        <input class="one-field-pardot-form-handler" maxlength="80" name="email" size="20" type="email" placeholder="Enter Email Address" required="required" />
        <div style="position:absolute; left:-9999px; top: -9999px;">
          <label for="pardot_extra_field">Comments</label>
          <input type="text" id="pardot_extra_field" name="pardot_extra_field">
        </div>

        <button class="g-recaptcha" data-sitekey="anonymous" data-callback="captchaSubmit" type="submit" name="captchaSubmit">Submit</button>
    </form>

后端:

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

    // Form info
    $email = $_POST["email"];
    $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_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 "Success!";
    else
        echo "Failure!";
?>

我之前曾使用下面的代码 posted 到 Pardot,但现在不清楚如何做到这一点,因为最初的 post 是 Google 而不是 Pardot。在 Invisible reCaptcha 成功确认后,我如何 post 到 Pardot?

<div class="nav-email-form">    
<form action="https://go.pardot.com/l/43312/2017-10-24/7dnr3n" method="post" class="pardot-email-form-handler" novalidate>

<input class="one-field-pardot-form-handler" maxlength="80" name="email" size="20" type="email" placeholder="Enter Email Address" required="required" />
<div style="position:absolute; left:-9999px; top: -9999px;">
  <label for="pardot_extra_field">Comments</label>
  <input type="text" id="pardot_extra_field" name="pardot_extra_field">
</div>

<button type="submit" name="submit">Submit</button>
</form>

由于您最初已经使用 curl 来处理验证码,也许您应该在 success 响应后使用 curl 向 Pardot 发出 POST 请求。你也许可以这样尝试 - 顺便说一句没有测试

function curl( $url=NULL, $options=NULL ){
    /*
        Download a copy of cacert.pem from
        https://curl.haxx.se/docs/caextract.html

        and then edit below as appropriate
    */
    $cacert='c:/wwwroot/cacert.pem';    #<-------- edit to suit own environment

    $res=array(
        'response'  =>  NULL,
        'info'      =>  array( 'http_code' => 100 ),
        'headers'   =>  NULL,
        'errors'    =>  NULL
    );
    if( is_null( $url ) ) return (object)$res;
    /* Initialise curl request object */
    $curl=curl_init();
    if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
    }
    /* Define standard options */
    curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
    curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $curl, CURLOPT_FAILONERROR, true );
    curl_setopt( $curl, CURLOPT_HEADER, false );
    curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' );
    curl_setopt( $curl, CURLOPT_ENCODING, '' );
    /* Assign runtime parameters as options */
    if( isset( $options ) && is_array( $options ) ){
        foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
    }
    /* Execute the request and store responses */
    $res=(object)array(
        'response'  =>  curl_exec( $curl ),
        'info'      =>  (object)curl_getinfo( $curl ),
        'errors'    =>  curl_error( $curl )
    );
    curl_close( $curl );
    return $res;
}

/*
    stage 1: POST to Google
    -----------------------
*/
$url='https://www.google.com/recaptcha/api/siteverify';
$params=array(
    'secret'    => $secret,
    'response'  => $_POST['g-recaptcha-response'],
    'remoteip'  => $_SERVER['REMOTE_ADDR']
);
$options=array(
    CURLOPT_POST        =>  true,
    CURLOPT_POSTFIELDS  =>  $params
);
$result=curl( $url, $options );
if( $result->info->http_code==200 ){

    $json=json_decode( $result->response );
    $status=$json->success;
    if( $status ){

        /*
            stage 2: POST to PARDOT
            -----------------------
        */

        $url='https://go.pardot.com/l/43312/2017-10-24/7dnr3n';

        /* fields within the PARDOT form */
        $_POST['pardot_extra_field']='';
        $_POST['submit']='';

        /* no need to send this field */
        unset( $_POST['g-recaptcha-response'] );

        /* this needs a value - but from where? */
        #$_POST['email']='GERONIMO@EXAMPLE.COM';

        $options=array(
            CURLOPT_POST        =>  true,
            CURLOPT_POSTFIELDS  =>  $_POST
        );
        $result=curl( $url, $options );
        if( $result->info->http_code==200 ){
            /* all good */
            header('Location: ?mailsent=true');
        } else {
            /* bogus */
        }
    } else {
        echo 'bogus';
    }
}

以下是我最终使用后端代码所做的事情。需要更好的成功和错误处理,我只将 SSL_VERIFYPEER 设置为 false 因为我是在本地环境中设置它,但它已经过测试并成功发布到 Pardot:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Results</title>
</head>
<body>
    <p>Thank you for entering the form.</p>

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

        // Form info
        $email = $_POST["email"];
        $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 "Success!";

            $pardotPost ='email='. $_POST["email"];
            $curl_handle = curl_init();
            $url = "anonymous";
            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 "Failure!";
        }
    ?>
</body>
</html>