由于对本地Web服务器的一些限制,我被迫使用cURL在远程服务器中处理我的评论表单数据。
我要实现的是:通过cURL将表单数据发送到远程验证脚本,远程验证脚本检查用户输入是否有错误。如果存在错误,则远程脚本应将“特定”错误发送回本地脚本。如果没有错误,我的远程验证脚本应该发送电子邮件给我,并输出一条成功消息,我应该在本地文件中接收该消息;如果提交成功或失败,我会将其输出给填写表单的用户。
这是我的本地文件Process.php的片段
$Email = $_POST['Email'];
$Comment = $_POST['Comment'];
$ch = curl_init();
$api ="http://RemoteServer.com/Sendmail.php";
$cu = "$api?Email=$Email&Comment=$Comment";
curl_setopt($ch,CURLOPT_URL,$cu);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT,20);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($ch);
curl_close($ch);
这也是我的远程文件http://RemoteServer.com/Sendmail.php
的摘要
/**************************************************/
//-- data and error arrays
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
/***************************************************/
/*********** CLEAN INPUTS **************************/
// Create function to clean input
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
/******************************************************/
/********************************* VALIDATION ******************************************************/
if ( !empty($_POST)) {
/***************** DEFINE $_POST Data ************************/
$Email = test_input($_POST["Email"]);
$Comment = test_input($_POST["Comment"]);
if (empty($Email)) {
$errors['Error'] = 'Enter your email';
} elseif (!filter_var($Email,FILTER_VALIDATE_EMAIL)) {
$errors['Error'] = 'Invalid email';
} elseif (empty($Comment)) {
$errors['Error'] = 'Enter a comment';
} else {
//Send email to myself and output success
$data['success'] = true;
}
if ( ! empty($errors)) {
// if there are items in our errors array,return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
$data['success'] = true;
// if there are no errors process our form,then return a message
}
// return all our data to an AJAX call
echo json_encode($data);
}//--END IF NOT EMPTY POST
else {
$data['success'] = false;
$data['errors'] = $errors;
}
现在,我希望实现的是:
在我的本地文件Process.php中,我应该能够从远程文件Sendmail.php接收错误并以这种方式使用它:
if (errors_from_remote_file) {
//--- Redirect user to error page to notify of form validation errors
header("Location: ./Error.php");
exit();
} else {
//--- Redirect user to Success page if form successfully validated and email sent to me
header("Location: ./Success.php");
exit();
}
此刻,我已经尝试过
if (isset($_POST))
and if (isset($_GET))
我都在远程文件Sendmail.php中,以从本地文件Process.php检索cURL发送的表单数据,但仍然无法获取表单数据。
我真的需要帮助,如何使用cURL检索从Process.php发送到Sendmail.php的帖子数据。
一旦实现,我还想知道如何从本地文件Process.php中的远程文件Sendmail.php中检索错误,并根据错误或成功情况使用它成功地将用户重定向到下一页输出到远程文件Sendmail.php
谢谢大家。