简体   繁体   English

验证表单cURL请求返回json响应

[英]Validate form cURL request return json response

Hi I'm trying to validate a form using a json output tha I get from a cURL request. 嗨,我正在尝试使用从cURL请求中获取的json输出来验证表单。 In my code I first check if the fields are empty or not, then I send the cURL request and I get the following json output if the fields are wrong 在我的代码中,我首先检查字段是否为空,然后发送cURL请求,如果字段错误,则会得到以下json输出

array(2) { ["errorDetailCode"]=> int(-44) ["errorDetailMessage"]=> string(37) "username or password not found" } 

array(2) { ["errorDetailCode"]=> int(-2) ["errorDetailMessage"]=> string(23) "username not found" }

array(2) { ["errorDetailCode"]=> int(-1) ["errorDetailMessage"]=> string(19) "password not found" }

The problem is after it shows the first message username and password not found then it doesn't show the other two messages: username not foundand password not found but still show the message: username or password not found. 问题是,它显示第一个消息未找到用户名和密码,然后不显示其他两个消息:未找到用户名和密码,但仍显示消息:未找到用户名或密码。 Do you know why? 你知道为什么吗? Ths is my code 这是我的代码

$km_username = filter_var($_POST['userName'], FILTER_SANITIZE_STRING);
$km_user_password = $_POST['userPassword'];

// Validate form fields if they are empty
if(empty($km_username) && empty($km_user_password)) {

        // Error message if email and password fields are empty
        $_SESSION['km_error_message'] = 'Insert username and password!';
        header('Location: '.KM_BASE_URL.'/login.php');
        exit();

}else if(empty($km_username)) {

        // Error message if username field is empty
        $_SESSION['km_error_message'] = 'Insert username!';
        header('Location: '.KM_BASE_URL.'/login.php');
        exit();

}else if(empty($km_user_password)) {

        // Error message if password field is empty
        $_SESSION['km_error_message'] = 'Insert password!';
        header('Location: '.KM_BASE_URL.'/login.php');
        exit();

}

// Store form fields into an array
$fields = array(
    'userid' => $km_username,
    'password' => $km_user_password
);

// cURL request to API
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, URL_LOGIN_API);
curl_setopt($cURL, CURLOPT_POST, 1);
curl_setopt($cURL, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($cURL, CURLOPT_HEADER, 0);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, TRUE);

$cURL_response = curl_exec($cURL); // execute the curl command

    if (curl_error($cURL)) {
        echo curl_error($cURL);
    }

curl_close ($cURL);

$json_response = json_decode($cURL_response, true);


// Form validation after cURL request

if(isset($json_response['errorDetailCode'])){

    // Error message if cURL request error
    $_SESSION['km_error_message'] = $json_response['errorDetailMessage'];
    header('Location: '.KM_BASE_URL.'/login.php');
    exit();

}else{

    // Store the cookie file name into the session
    if (!isset($_SESSION['cookiefile'])) {
        $cookiefile = tempnam(".", "cookie");
        $_SESSION['cookiefile'] = basename($cookiefile);
        file_put_contents($cookiefile, "");
    }

    // cURL request to API
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, URL_LOGIN_API);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); // Cookie aware
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); // Cookie aware
    $content = curl_exec($ch);
    curl_close($ch);

    // Redirerct user to dashboard
    header('Location: '.KM_BASE_URL.'/client-dashboard.php');
    exit();

}

Looking at your code, it seems to me that you are checking if the decoded array has the key 'errorDetailCode', if the condition is met, send a header and terminate the script. 查看您的代码,在我看来,您正在检查解码后的数组是否具有键“ errorDetailCode”,如果满足条件,请发送标头并终止脚本。

The message inside the first array you got back from cURL will be sent within the header, but after that there is the exit() call, the script is over, and it can not send or print anything more. 您从cURL返回的第一个数组内的消息将在标头中发送,但是此后有exit()调用,脚本结束了,它无法发送或打印任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM