🔴 Advanced · Lesson 37
REST API
REST API
What is a REST API?
A REST API lets other apps talk to your server using HTTP methods (GET, POST, PUT, DELETE) and returns data as JSON.
Simple REST Endpoint
header("Content-Type: application/json");
$method = $_SERVER["REQUEST_METHOD"];
if ($method === "GET") {
echo json_encode(["users" => ["Aman", "Riya"]]);
} elseif ($method === "POST") {
$input = json_decode(file_get_contents("php://input"), true);
echo json_encode(["created" => $input]);
}
HTTP Methods
| Method | Use |
|---|---|
| GET | read data |
| POST | create data |
| PUT | update data |
| DELETE | remove data |
Summary
- A REST API uses HTTP methods and returns JSON.
- Read the method from
$_SERVER["REQUEST_METHOD"]and respond with json_encode.
REST API क्या है?
REST API दूसरे apps को HTTP methods (GET, POST, PUT, DELETE) से आपके server से बात करने देता है और data JSON में लौटाता है।
Simple REST Endpoint
header("Content-Type: application/json");
$method = $_SERVER["REQUEST_METHOD"];
if ($method === "GET") {
echo json_encode(["users" => ["Aman", "Riya"]]);
} elseif ($method === "POST") {
$input = json_decode(file_get_contents("php://input"), true);
echo json_encode(["created" => $input]);
}
HTTP Methods
| Method | Use |
|---|---|
| GET | data पढ़ना |
| POST | data बनाना |
| PUT | data update |
| DELETE | data हटाना |
सारांश
- REST API HTTP methods use करता है और JSON लौटाता है।
- Method
$_SERVER["REQUEST_METHOD"]से पढ़ें और json_encode से respond करें।