【发布时间】:2019-11-05 13:31:10
【问题描述】:
由于对我的本地 Web 服务器的一些限制,我不得不使用 cURL 在远程服务器中处理我的评论表单数据。
我想要实现的是:通过 cURL 将表单数据发送到远程验证脚本,远程验证脚本检查用户输入是否有错误。如果有错误,远程脚本应该将“特定”错误发送回本地脚本。如果没有错误,我的远程验证脚本应该向我发送电子邮件并输出我应该在本地文件中收到的成功消息,如果提交成功与否,我将向填写表单的用户输出相同的消息。
这是我本地名为 Process.php 的文件的 sn-p
$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的sn-p@
/**************************************************/
//-- 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 中输出
谢谢大家。
【问题讨论】:
-
首先,您从本地脚本将输入作为参数发送,但尝试通过远程服务器上的 $_POST 访问它。其次,您不会在远程脚本的 ELSE 部分返回错误。
-
@djunehor 如果我使用参数,那么如何在远程文件中检索?其次,远程文件的 else 部分只能有一个错误,即如果 sendmail 失败,但这并不重要,因为我可以写出来。重要的是我之前在问题中提到的几点。
-
“我真的需要关于如何检索发布数据的帮助” - 从实际发出一个 POST 请求开始吧?关于如何在 PHP 中使用 cURL 的详细信息,您应该可以轻松研究自己。
-
“我还想知道如何从本地文件 Process.php 中的远程文件 Sendmail.php 中检索错误” - 您可以访问响应正文$结果。由于您要发送 JSON,下一步将是对其进行解码,以便您可以再次访问数据结构中的元素,然后可以根据这些决定做什么。
-
@04FS 感谢您的来信。然而,英语不是我的第一语言,也许我没有正确表达。当然,我确实知道如何检索基本的帖子数据。检查我的远程文件,看看我已经检索到 $_POST 数据。我的经验是我无法从 $_POST 中获取数据,而且我不知道我是否在 CURL 请求中传递了正确的参数。至于 localfile 中的 $result,我也尝试解码响应,但还没有 yield。