PHP實現(xiàn) RESTful 風格的 API
目錄詳情
restful/
- Request.php 數(shù)據(jù)操作類
- Response.php 輸出類
- index.php 入口文件
- .htaccess 重寫url
Request.php
包含一個Request類旦签,即數(shù)據(jù)操作類缺猛。
-
接收到URL的數(shù)據(jù)后儡司,根據(jù)請求URL的方式(GET|POST|PUT|PATCH|DELETE)對數(shù)據(jù)進行相應的增刪改查操作限匣,并返回操作后的結(jié)果
<?php /** * Request類, * 接收到URL的數(shù)據(jù)后彤守,根據(jù)請求URL的方式(GET|POST|PUT|PATCH|DELETE)對數(shù)據(jù)進行相應的增刪改查操作徙融,并返回操作后的結(jié)果 */ class Request { // 允許的請求方式 private static $method_type = array('get', 'post', 'put', 'patch', 'delete'); // 測試數(shù)據(jù) private static $test_class = array( 1 => array('name'=>'測試一班','count'=>18), 2 => array('name'=>'測試二班','count'=>15) ); public static function getRequest() { // 請求方法 $method = strtolower($_SERVER['REQUEST_METHOD']); if (in_array($method, self::$method_type)) { // 調(diào)用請求方法對應的方法 $data_name = $method . "Data"; return self::$data_name($_REQUEST); } return false; } // GET 獲取信息 private static function getData($request_data) { $class_id = (int)$request_data['class']; if ($class_id > 0) { // GET /class/ID: 獲取某個指定班的信息 return self::$test_class[$class_id]; }else{ // GET /class: 列出所有班級 return self::$test_class; } } // POST /class 新建一個班級 private static function postData($request_data) { $class_id = (int)$request_data['class']; if ($class_id == 0) { return false; } $data = array(); if (!empty($request_data['name']) && isset($request_data['count'])) { $data['name'] = $request_data['name']; $data['count'] = $request_data['count']; self::$test_class[] = $data; return self::$test_class; }else{ return false; } } // PUT /class/ID 更新某個指定班級的信息(全部信息) private static function putData($request_data) { $class_id = (int)$request_data['class']; if ($class_id == 0) { return false; } $data = array(); if (!empty($request_data['name']) && isset($request_data['count'])) { $data['name'] = $request_data['name']; $data['count'] = (int)$request_data['count']; self::$test_class[$class_id] = $data; return self::$test_class; }else{ return false; } } // PATCH /class/ID 更新某個指定班級的信息 (部分信息) private static function pacthData($request_data) { $class_id = (int)$request_data['class']; if ($class_id == 0) { return false; } if (!empty($request_data['name'])) { self::$test_class[$class_id]['name'] = $request_data['name']; } if (isset($request_data['count'])) { self::$test_class[$class_id]['count'] = $request_data['count']; } return self::$test_class; } // DELETE /class/ID 刪除某個班 private static function deleteData($request_data) { $class_id = (int)$request_data['class']; if ($class_id == 0) { return false; } unset(self::$test_class[$class_id]); return self::$test_class; } } ?>
Response.php
包含一個Response類洒缀,即輸出類。
-
根據(jù)接收到的Content-Type欺冀,將Request類返回的數(shù)組拼接成對應的格式树绩,加上header后輸出
<?php /** * 包含一個Response類,即輸出類隐轩。根據(jù)接收到的Content-Type饺饭,將Request類返回的數(shù)組拼接成對應的格式,加上header后輸出 */ class Response { const HTTP_VERSION = "HTTP/1.1"; public function sendResponse($data) { // 獲取數(shù)據(jù) if ($data) { $code = 200; $message = "OK"; }else{ $code = 404; $data = array('error' => "Not Found"); $message = "Not Found"; } header(self::HTTP_VERSION . " $code $message"); $content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : $_SERVER['HTTP_ACCEPT']; if (strpos($content_type, 'application/json') !== false) { header("Content-Type: application/json"); echo self::encodeJson($data); }elseif (strpos($content_type, 'application/xml') !== false) { header("Content-Type: application/xml"); echo self::encodeXml($data); }else{ header("Content-Type: text/html"); echo self::encodeHtml($data); } } // json 格式 private static function encodeJson($responseData) { return json_encode($responseData); } // xml 格式 private static function encodeXml($responseData) { $xml = new SimpleXMLElement('<?xml version="1.0"?><rest></rest>'); foreach ($responseData as $key => $value) { if (is_array($value)) { foreach ($value as $k => $v) { $xml->addChild($k,$v); } }else{ $xml->addChild($key,$value); } } return $xml->asXML(); } // html 格式 private static function encodeHtml($responseData) { $html = "<table border='1'>"; foreach ($responseData as $key => $value) { $html .= "<tr>"; if (is_array($value)) { foreach ($value as $k => $v) { $html .= "<td>$k</td><td>$v</td>"; } }else{ $html .= "<td>$key</td><td>$value</td>"; } $html .= "</tr>"; } $html .="</table>"; return $html; } } ?>
index.php
- 入口文件
- 調(diào)用 Request->getRequest 獲取數(shù)據(jù)
- 調(diào)用 Response->sendResponse 處理并返回數(shù)據(jù)
.htaccess
-
重寫URL职车,使URL以 /restful/class/1 形式訪問文件
Options +FollowSymlinks RewriteEngine on # 重寫規(guī)則 RewriteRule ^class$ index.php?class=all [nc,qsa] RewriteRule ^class/(\d+)$ index.php?class=$1 [nc,qsa]
測試方法
- 采用 postman 測試
GET
- 獲取 json 類型
- 獲取 xml
- 獲取 html