【问题标题】:Send form data using cURL and receive JSON validation error from remote API使用 cURL 发送表单数据并从远程 API 接收 JSON 验证错误
【发布时间】: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。

标签: php json curl


【解决方案1】:

感谢到目前为止所有回复的人。对于$_POST 错误,我发现我缺少semicolon。排序后,我就能够正确检索 POST DATA。

对于来自远程文件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 上阅读一些 tuts 是一个漫长的过程。但是,我浏览了 JSON 上的几行代码,就可以自己完成了。一开始很沮丧,但我很高兴我通过自学来艰难地学习。

【讨论】:

    猜你喜欢
    • 2012-08-31
    • 2014-11-02
    • 2016-11-15
    • 2015-12-15
    • 1970-01-01
    • 2020-09-25
    • 2021-07-21
    • 2016-05-16
    • 2019-07-29
    相关资源
    最近更新 更多