我有一个可能是一个非常基本的问题,但我对PHP和表单创建很新,所以希望有人可以帮助我。
我们有垃圾邮件机器人在我们的网站上不断提交单字段电子邮件表格,尽管有蜜罐方法,所以我希望使用谷歌的Invisible reCaptcha来解决这个问题。
我按照本有用指南中的说明操作:https://www.pinnacleinternet.com/installing-invisible-recaptcha/但是我遇到困难的是,在结果成功之后,我想要获取通过表单提交的电子邮件地址然后将其发布到第三方服务器(在这种情况下,我们的营销自动化工具,Pardot)。
这是Invisible reCaptcha代码:
前端
<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!";
?>
我之前使用下面的代码发布到Pardot,但我现在还不清楚如何做到这一点,因为最初的帖子是谷歌而不是Pardot。在Invisible reCaptcha成功确认后,我如何向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>
答案 0 :(得分:1)
由于您最初已经使用curl
处理验证码,因此您应该使用curl发出POST
请求,以便在success
响应时进行Pardot。你也许可以尝试这样 - 没有测试btw
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';
}
}
答案 1 :(得分:0)
以下是我最后使用后端代码所做的事情。需要更好的成功&amp;错误处理,我只将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>