Привет. Я пытаюсь проверить форму, используя вывод json, полученный из запроса cURL.В моем коде я сначала проверяю, являются ли поля пустыми или нет, затем я отправляю запрос 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" }
Проблема в том, что после того, как оно показывает первое сообщениеимя пользователя и пароль не найдены, тогда не отображаются два других сообщения: имя пользователя не найдено и пароль не найден, но все равно отображается сообщение: имя пользователя или пароль не найдены.Ты знаешь почему?Это мой код
$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();
}