RESTful API是一种软件架构风格
RESTful API基于HTTP协议,并遵循一系列约定和原则。它的设计理念是将资源(Resource)作为核心概念,并通过一组统一的接口对资源进行操作。API的资源通常通过URL进行标识,而HTTP方法(如GET、POST、PUT、DELETE)则用于定义对这些资源的不同操作。

-
- // 获取请求的URL路径和方法
- $requestUrl = $_SERVER['REQUEST_URI'];
- $requestMethod = $_SERVER['REQUEST_METHOD'];
-
- // 处理请求
- if ($requestMethod === 'GET') {
- handleGetRequest($requestUrl);
- } elseif ($requestMethod === 'POST') {
- handlePostRequest($requestUrl);
- } elseif ($requestMethod === 'PUT') {
- handlePutRequest($requestUrl);
- } elseif ($requestMethod === 'DELETE') {
- handleDeleteRequest($requestUrl);
- } else {
- sendResponse(405, 'Method Not Allowed');
- }
-
- // 处理GET请求
- function handleGetRequest($requestUrl) {
- if ($requestUrl === '/users') {
- $users = ['user1', 'user2', 'user3'];
- sendResponse(200, $users);
- } elseif (preg_match('/\/users\/(\d+)/', $requestUrl, $matches)) {
- $userId = $matches[1];
- $user = getUserById($userId);
- if ($user) {
- sendResponse(200, $user);
- } else {
- sendResponse(404, 'User not found');
- }
- } else {
- sendResponse(404, 'Not Found');
- }
- }
- // 处理POST请求
- function handlePostRequest($requestUrl) {
- if ($requestUrl === '/users') {
- $username = $_POST['username'];
- // 处理创建用户逻辑
- sendResponse(201, 'User created successfully');
- } else {
- sendResponse(404, 'Not Found');
- }
- }
- // 处理PUT请求
- function handlePutRequest($requestUrl) {
- if (preg_match('/\/users\/(\d+)/', $requestUrl, $matches)) {
- $userId = $matches[1];
- $user = getUserById($userId);
- if ($user) {
- // 处理更新用户逻辑
- sendResponse(200, 'User updated successfully');
- } else {
- sendResponse(404, 'User not found');
- }
- } else {
- sendResponse(404, 'Not Found');
- }
- }
- // 处理DELETE请求
- function handleDeleteRequest($requestUrl) {
- if (preg_match('/\/users\/(\d+)/', $requestUrl, $matches)) {
- $userId = $matches[1];
- $user = getUserById($userId);
- if ($user) {
- // 处理删除用户逻辑
- sendResponse(200, 'User deleted successfully');
- } else {
- sendResponse(404, 'User not found');
- }
- } else {
- sendResponse(404, 'Not Found');
- }
- }
-
- // 获取请求的URL路径和方法
- $requestUrl = $_SERVER['REQUEST_URI'];
- $requestMethod = $_SERVER['REQUEST_METHOD'];
-
- // 处理请求
- if ($requestMethod === 'GET') {
- handleGetRequest($requestUrl);
- } elseif ($requestMethod === 'POST') {
- handlePostRequest($requestUrl);
- } elseif ($requestMethod === 'PUT') {
- handlePutRequest($requestUrl);
- } elseif ($requestMethod === 'DELETE') {
- handleDeleteRequest($requestUrl);
- } else {
- sendResponse(405, 'Method Not Allowed');
- }
-
- // 处理GET请求
- function handleGetRequest($requestUrl) {
- if ($requestUrl === '/users') {
- $users = ['user1', 'user2', 'user3'];
- sendResponse(200, $users);
- } elseif (preg_match('/\/users\/(\d+)/', $requestUrl, $matches)) {
- $userId = $matches[1];
- $user = getUserById($userId);
- if ($user) {
- sendResponse(200, $user);
- } else {
- sendResponse(404, 'User not found');
- }
- } else {
- sendResponse(404, 'Not Found');
- }
- }
-
- // 处理POST请求
- function handlePostRequest($requestUrl) {
- if ($requestUrl === '/users') {
- $username = $_POST['username'];
- // 处理创建用户逻辑
- sendResponse(201, 'User created successfully');
- } else {
- sendResponse(404, 'Not Found');
- }
- }
-
- // 处理PUT请求
- function handlePutRequest($requestUrl) {
- if (preg_match('/\/users\/(\d+)/', $requestUrl, $matches)) {
- $userId = $matches[1];
- $user = getUserById($userId);
- if ($user) {
- // 处理更新用户逻辑
- sendResponse(200, 'User updated successfully');
- } else {
- sendResponse(404, 'User not found');
- }
- } else {
- sendResponse(404, 'Not Found');
- }
- }
-
- // 处理DELETE请求
- function handleDeleteRequest($requestUrl) {
- if (preg_match('/\/users\/(\d+)/', $requestUrl, $matches)) {
- $userId = $matches[1];
- $user = getUserById($userId);
- if ($user) {
- // 处理删除用户逻辑
- sendResponse(200, 'User deleted successfully');
- } else {
- sendResponse(404, 'User not found');
- }
- } else {
- sendResponse(404, 'Not Found');
- }
- }
-
- // 根据ID获取用户信息
- function getUserById($userId) {
- // 获取用户的逻辑
- $users = [
- 1 => 'user1',
- 2 => 'user2',
- 3 => 'user3'
- ];
- return isset($users[$userId]) ? $users[$userId] : null;
- }
-
- // 发送响应
- function sendResponse($statusCode, $data) {
- header('Content-Type: application/json');
- http_response_code($statusCode);
- echo json_encode($data);
- }