dongshao1981 2018-10-04 08:01
浏览 191

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

Hi I'm trying to validate a form using a json output tha I get from a cURL request. 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

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();

}
  • 写回答

1条回答 默认 最新

  • douwen9343 2018-10-04 08:22
    关注

    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.

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)