我在一个页面上自动渲染小部件。在值应该使用POST的页面上,我的PHP代码是:
<?php
require_once('recaptchalib.php');
if(isset($_POST['submit']) && !empty($_POST['submit'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
$data = array(
'secret' => "secret-key",
'response' => $_POST['g-recaptcha-response']
);
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
var_dump($response);
if(var_dump($response)->success):
//contact form submission code
$name = !empty($_POST['name'])?$_POST['name']:'';
$email = !empty($_POST['email'])?$_POST['email']:'';
$phone = !empty($_POST['phonenumber'])?$_POST['phonenumber']:'';
$message = !empty($_POST['comment'])?$_POST['comment']:'';
$to = 'jdoe@mail.com';
$subject = 'Personal Message from '.$name;
$htmlContent = "
<h1>Contact Us - Message</h1>
<p style=\"font-size: 1.5em;\"><span style=\"font-weight: bolder;\">Name: </span>".$name."</p>
<p style=\"font-size: 1.5em;\"><span style=\"font-weight: bolder;\">Email: </span>".$email."</p>
<p style=\"font-size: 1.5em;\"><span style=\"font-weight: bolder;\">Phone Number: </span>".$phone."</p>
<p style=\"font-size: 1.5em;\"><span style=\"font-weight: bolder;\">Message: </span>".$message."</p>";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
//send email
@mail($to,$subject,$htmlContent,$headers);
$succMsg = '<em>Your contact request submitted successfully.</em>';
echo $succMsg;
else:
$errMsg = '<em>Robot verification failed, please try again.</em>';
echo $errMsg;
endif;
else:
$errMsg = '<em>Please click on the reCAPTCHA box.</em>';
echo $errMsg;
endif;
else:
$errMsg = 'Something went wrong with the submission';
$succMsg = '';
echo $errMsg;
endif;
?>
我提交表单时在发布的页面上输出的消息是:
string(95)“{”success“:true,”challenge_ts“:”2016-12-12T15:38:25Z“,”hostname“:”mywebsite.com“}”string(95)“{”成功“:true,”challenge_ts“:”2016-12-12T15:38:25Z“,”主机名“:”mywebsite.com“}”机器人验证失败,请再试一次。
我不太确定字符串(95)的含义以及为什么它输出JSON信息,当它看起来成功是真的。我不知道为什么这个页面输出这个的基础知识。此外,电子邮件也不会被发送,这是另一个问题。