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'] != '') { // get tag $tag = $_POST['tag']; // include db handler require_once 'include/DB_Functions.php'; $db = new DB_Functions(); // response Array $response = array("tag" => $tag, "error" => FALSE); // check for tag type if ($tag == '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); } } else if ($tag == 'register') { // Request type is Register new user $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; // 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); 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"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo json_encode($response); } else { // user failed to store $response["error"] = TRUE; $response["error_msg"] = "Error occurred in registration"; echo json_encode($response); } } } else if ($tag == '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); } else if ($tag = '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); } } else if($tag == '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); } else { // user failed to store $response["error"] = TRUE; $response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'"; echo json_encode($response); } } else { $response["error"] = TRUE; $response["error_msg"] = "Required parameter 'tag' is missing!"; echo json_encode($response); } ?>
Output for 5.4.0 - 7.2.6
{"error":true,"error_msg":"Required parameter 'tag' is missing!"}