Request.php :包含一个Request类,即数据操作类。接收到URL的数据后,根据请求URL的方式(GET|POST|PUT|PATCH|DELETE)对数据进行相应的增删改查操作,并返回操作后的结果:
<?php
/**
* 数据操作类
*/
class Request {
//允许的请求方式
private static $method_type = array('get', 'post', 'put', 'patch', 'delete');
//测试数据
private static $test_class = array(
1 => array('name' => '托福班', 'count' => 18),
2 => array('name' => '雅思班', 'count' => 20),
);
public static function getRequest() {
//请求方式
$method = strtolower($_SERVER['REQUEST_METHOD']);
if (in_array($method, self::$method_type)) {
//调用请求方式对应的方法
$data_name = $method . 'Data';
return self::$data_name($_REQUEST);
}
return false;
}
//GET 获取信息
private static function getData($request_data) {
$class_id = (int)$request_data['class'];
//GET /class/ID:获取某个指定班的信息
if ($class_id > 0) {
return self::$test_class[$class_id];
} else {//GET /class:列出所有班级
return self::$test_class;
}
}
//POST /class:新建一个班
private static function postData($request_data) {
if (!empty($request_data['name'])) {
$data['name'] = $request_data['name'];
$data['count'] = (int)$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 patchData($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'] = (int)$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 :包含一个Request类,即输出类。根据接收到的Content-Type,将Request类返回的数组拼接成对应的格式,加上header后输出.
<?php
class Response {
const HTTP_VERSION = "HTTP/1.1";
//返回结果
public static function sendResponse($data) {
//获取数据
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);
} else if (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 :入口文件,调用Request类取得数据后交给Response处理,最后返回结果
<?php
//数据操作类
require('Request.php');
//输出类
require('Response.php');
//获取数据
$data = Request::getRequest();
//输出结果
Response::sendResponse($data);
标签:return,name,RESTful,self,request,API,PHP,data,class
From: https://blog.51cto.com/u_15979955/7346034