首页 > 解决方案 > 使用 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

谢谢大家。

标签: phpjsoncurl

解决方案


Thanks to everyone who has responded so far. For the $_POST error, I found out I was missing a semicolon. Once I had it sorted, I was able to retrieve the POST DATA correctly.

As for the JSON result from the Remote File Sendmail.php, I was able to output the errors or success notice by decoding the JSON object and used it to manipulate location to direct the user to upon error or upon success. Here is sample code used in my localfile:

   $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();
}
//---------------------------------------------

I made basic errors by not watching out for the missing semicolon and I also thought it was a long process to read a few tuts on JSON. But, I glanced through a few lines on JSON and I was able to get this done myself. Frustrating initially but I am happy I learnt the hard way by learning on my own.


推荐阅读