A REST API (Representational State Transfer) is a way for two systems to communicate using the HTTP protocol. It allows clients such as browsers, mobile apps, or other servers to interact with a backend by sending requests and receiving structured responses, commonly in JSON format.
// Common HTTP methods used in REST APIs
GET → Retrieve data
POST → Create new data
PUT → Update existing data
DELETE → Remove data
PATCH → Update part of the resource
// Simple PHP REST API returning JSON data
<?php
header("Content-Type: application/json");
$users = [
["id" => 1, "name" => "Alice"],
["id" => 2, "name" => "Bob"],
["id" => 3, "name" => "Charlie"]
];
if ($_SERVER["REQUEST_METHOD"] === "GET") {
echo json_encode($users);
}
?>
// Example GET request to fetch users
GET http://localhost/api/users
// JSON response returned by the API
[
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" },
{ "id": 3, "name": "Charlie" }
]
This API can be tested interactively using tools like browser address bars, REST clients, or JavaScript fetch() calls to visualize real-time responses.
POST API to add a new user.PUT and DELETE to update and remove data.