PHP REST APIs
A REST API uses HTTP methods (GET, POST, PUT, DELETE) to expose resources as URLs. PHP handles routing, validation, business logic, and JSON responses.
A REST API uses HTTP methods (GET, POST, PUT, DELETE) to expose resources as URLs. PHP handles routing, validation, business logic, and JSON responses.
// Minimal REST endpoint in plain PHP
header("Content-Type: application/json");
$method = $_SERVER["REQUEST_METHOD"];
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
if ($method === "GET" && $path === "/api/users") {
echo json_encode(["data" => getUsers()]);
} else {
http_response_code(404);
echo json_encode(["error" => "Not found"]);
}
For anything beyond a few endpoints, use a framework (Laravel, Slim) rather than manual routing.