使用cURL发送表单数据,并从远程API接收JSON验证错误



由于本地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发送的表单数据,但我仍然无法获取表单数据。

我真的需要帮助,如何检索从Process.php发送的帖子数据使用cURL发送到Sendmail.php。

一旦实现了这一点,我还想知道如何在本地文件Process.php中从远程文件Sendmail.php中检索错误,并使用它成功地将用户重定向到下一页,具体取决于远程文件Sendmail.php 中的错误或成功输出

感谢大家。

感谢迄今为止做出回应的所有人。对于$_POST错误,我发现我缺少一个semicolon。一旦我对它进行了排序,我就能够正确地检索POST数据。

至于来自远程文件Sendmail.php的JSON结果,我能够通过解码JSON对象来输出错误或成功通知,并使用它来操纵位置,以在错误或成功时引导用户。以下是我的本地文件中使用的示例代码:

$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w'); //-- To monitor cURL procedure
$data = array("name"=>"ohidul","age"=>20);
$string = http_build_query($data);
$ch = curl_init("http://RemoteServer.com/Sendmail.php");
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);
curl_exec($ch);
$response = curl_exec($ch);
curl_close($ch);
//-------------DECODE JSON ERROR FROM REMOTE FILE---------------------------------  
$RemoteResponse = json_decode($response);
if ($RemoteResponse == "Failed") {
header("Location: ./Error.php"); 
exit();
} else {
header("Location: ./Success.php"); 
exit();
}
//---------------------------------------------

我犯了一些基本错误,因为我没有注意到丢失的分号,我还认为阅读一些JSON教程是一个漫长的过程。但是,我浏览了JSON上的几行代码,就可以自己完成了。起初很沮丧,但我很高兴我通过自学艰难地学会了。

最新更新