In PHP authorization systems, how do I expose a REST API?

In PHP authorization systems, exposing a REST API allows clients to access and manipulate resources securely. Here's a simple example of how to create a REST API in PHP that handles user authentication and returns JSON responses.

<?php header('Content-Type: application/json'); // Sample user data for authentication $users = [ ['username' => 'user1', 'password' => 'pass1'], ['username' => 'user2', 'password' => 'pass2'], ]; // Function to authenticate user function authenticate($username, $password) { global $users; foreach ($users as $user) { if ($user['username'] === $username && $user['password'] === $password) { return true; } } return false; } // Handle incoming requests if ($_SERVER['REQUEST_METHOD'] === 'POST') { $requestData = json_decode(file_get_contents('php://input'), true); $username = $requestData['username']; $password = $requestData['password']; if (authenticate($username, $password)) { echo json_encode(['message' => 'Authentication successful']); } else { echo json_encode(['message' => 'Authentication failed'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } } else { echo json_encode(['message' => 'Invalid request method']); } ?>

PHP REST API Authorization User Authentication JSON Response