⚙️ Backend
API Development
Last updated: 2025-09-25 02:02:49
API Development Best Practices
APIs (Application Programming Interfaces) enable communication between different software systems.
REST API Principles
- GET - Retrieve data
- POST - Create new resource
- PUT - Update entire resource
- PATCH - Partial update
- DELETE - Remove resource
PHP API Example
<?php
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
$method = $_SERVER["REQUEST_METHOD"];
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
switch ($method) {
case "GET":
if ($path === "/api/users") {
echo json_encode(["users" => getUsersFromDB()]);
}
break;
case "POST":
$input = json_decode(file_get_contents("php://input"), true);
$result = createUser($input);
echo json_encode($result);
break;
}
?>
JavaScript Fetch API
// GET request
fetch("/api/users")
.then(response => response.json())
.then(data => console.log(data));
// POST request
fetch("/api/users", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: "John Doe",
email: "john@example.com"
})
})
.then(response => response.json())
.then(data => console.log(data));