3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * File to handle all API requests * Accepts GET and POST * * Each request will be identified by TAG * Response will be JSON data /** * check for POST request */ if (isset($_POST['tag']) && $_POST['tag'] != '') { // include db handler require_once 'include/DB_Functions.php'; $db = new DB_Functions(); // get tag value $tag = $_POST['tag']; // response Array $response = array("tag" => $tag, "error" => FALSE); switch($tag){ case 'login': /** Request type is check Login**/ $email = $_POST['email']; $password = $_POST['password']; /** check for user**/ $user = $db->getUserByEmailAndPassword($email, $password); if ($user != false) { // user found $response["error"] = FALSE; $response["user"]["unique_id"] = $user["unique_id"]; $response["user"]["name"] = $user["name"]; $response["user"]["email"] = $user["email"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; $response["user"]["StudentID"] = $user["StudentID"]; echo json_encode($response); } else { /** user not found * echo json with error = 1**/ $response["error"] = TRUE; $response["error_msg"] = "Incorrect email or password!"; echo json_encode($response); } break; case 'register': /** Request type is Register new user**/ $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; $phoneNumber = $_POST['phoneNumber']; /** check if user is already existed**/ if ($db->isUserExisted($email)) { // user is already existed - error response $response["error"] = TRUE; $response["error_msg"] = "User already existed"; echo json_encode($response); } else { // store user $user = $db->storeUser($name, $email, $password, $phoneNumber); if ($user) { // user stored successfully $response["error"] = FALSE; $response["uid"] = $user["unique_id"]; $response["user"]["name"] = $user["name"]; $response["user"]["email"] = $user["email"]; $response["user"]["StudentID"]=$user["StudentID"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; $response["user"]["phoneNumber"] = $user["phoneNumber"]; echo json_encode($response); } else { // user failed to store $response["error"] = TRUE; $response["error_msg"] = "Error occurred in registration"; echo json_encode($response); } } break; case 'getCourses': $user = FALSE; if(isset ($_POST['StudentID'])) $user = $_POST['StudentID']; $selectionKey = $_POST['searchKey']; $selectionValue = $_POST['searchValue']; $response = $db->getCourses($user, $selectionKey, $selectionValue); if($response) $response["error"] = FALSE; else { $response["error"]=TRUE; $response["error_mesg"] = "Error occurred getting the courses"; } echo json_encode($response); break; case 'addCourse': // Request type is add a course to a specific student $StudentID = $_POST['StudentID']; $CourseID = $_POST['CourseID']; $email = $_POST['email']; // check if user is already existed if ($db->isUserExisted($email)) { // store course $course = $db->storeCourse($StudentID, $CourseID); if ($course !=FALSE) { // course stored successfully $response["error"] = FALSE; $response["course"]["StudentID"] = $course["StudentID"]; $response["course"]["CourseID"] = $course["CourseID"]; echo json_encode($response); } else { // course failed to store $response["error"] = TRUE; $response["error_msg"] = "Error occured adding courses"; echo json_encode($response); } } else{ $response["error"] = TRUE; $response["error_msg"] = "User does not exist"; echo json_encode($response); } break; case 'getFriends': $courseID = $_POST['courseID']; $studentID = $_POST['studentID']; $result = $db->getFriendsByCourseIdAndStudentId($courseID, $studentID); if($result !=FALSE) $result['error']=FALSE; else{ $result['error']=TRUE; $result["error_mesg"] = "Error occurred retrieving friends"; } echo json_encode($result); break; default: // user failed to store $response["error"] = TRUE; $response["error_msg"] = "Unknow 'tag' value"; echo json_encode($response); break; } $response["error"] = TRUE; $response["error_msg"] = "Required parameter 'tag' is missing!"; echo json_encode($response); } ?>
based on hLBnY
Output for 5.4.0 - 7.2.0