使用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发送的表单数据,但仍然无法获取表单数据。

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

一旦实现,我还想知道如何从本地文件Process.php中的远程文件Sendmail.php中检索错误,并根据错误或成功情况使用它成功地将用户重定向到下一页输出到远程文件Sendmail.php

谢谢大家。

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

感谢迄今已做出回应的每个人。对于$_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,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,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的几行内容,然后自己完成了此操作。最初令人沮丧,但我很高兴能通过独自学习来学习艰难的方法。

本文链接:https://www.f2er.com/3159253.html

大家都在问