Create REST API

A

Anonymous

Guest
QNS

I'm having an issue so l managed to answer the first part of just creating an API that returns the data in JSON format but now I'm confused with the last part that is below where l need to add sessionID if the user is logged out.

Create an api that return the SessionID,storeID and ChannelCode where
outTS column is 0000-00-00 00:00:0


Code:
<?php

// displaying errors faced
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// call the database file
include('includes/db.inc.php');

//response
$response = array();

// select the data from the table checkinSession
$sql_query = "SELECT sessionID, storeID, channelCode FROM checkinSession WHERE outTS = '0000-00-00 00:00:00'";

$sql_data = mysqli_query($con, $sql_query);

// fetching data as an associative array
if($sql_data){
    $i = 0; 
    while($sql_data_arr = mysqli_fetch_assoc($sql_data)){
        $response[$i]['sessionID'] = $sql_data_arr['sessionID'];
        $response[$i]['storeID'] = $sql_data_arr['storeID'];
        $response[$i]['channelCode'] = $sql_data_arr['channelCode'];
        $i++;
    };

    // OUTPUT DATA (JSON)
    header("Content-Type: application/json");
    echo json_encode($response, JSON_PRETTY_PRINT);
}
else{
    // OUTPUT DATA (JSON)
    header("Content-Type: application/json");
    echo json_encode("nothing returned", JSON_PRETTY_PRINT);
}

?>


Second part of the Qns

After that add the sessionID when the user is log off(0000-00-00 00:00:00) to the api
 
To add the condition for the session ID when the user is logged out, you can modify your code as follows:


<?php
// displaying errors faced
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// call the database file
include('includes/db.inc.php');

//response
$response = array();

// check if the user is logged out
if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] === false) {
$response['sessionID'] = 'User logged out';
} else {
// select the data from the table checkinSession
$sql_query = "SELECT sessionID, storeID, channelCode FROM checkinSession WHERE outTS = '0000-00-00 00:00:00'";

$sql_data = mysqli_query($con, $sql_query);

// fetching data as an associative array
if ($sql_data) {
$i = 0;
while ($sql_data_arr = mysqli_fetch_assoc($sql_data)) {
$response[$i]['sessionID'] = $sql_data_arr['sessionID'];
$response[$i]['storeID'] = $sql_data_arr['storeID'];
$response[$i]['channelCode'] = $sql_data_arr['channelCode'];
$i++;
}

// OUTPUT DATA (JSON)
header("Content-Type: application/json");
echo json_encode($response, JSON_PRETTY_PRINT);
} else {
// OUTPUT DATA (JSON)
header("Content-Type: application/json");
echo json_encode("nothing returned", JSON_PRETTY_PRINT);
}
}
?>

n this code, we first check if the $_SESSION['loggedIn'] variable is set and if it is set to false, indicating that the user is logged out. If that condition is true, we set the sessionID value in the response array to "User logged out".

If the user is not logged out, the code proceeds to execute the SQL query and fetches the data as before. The response array is populated with the sessionID, storeID, and channelCode values retrieved from the database. Finally, the response is encoded in JSON format and sent back to the client.

Make sure to handle the user login and session management elsewhere in your application and set $_SESSION['loggedIn'] accordingly based on the user's login status.
 
Back
Top