index.php
<?php include './Request.php'; include './Response.php'; //获取数据 $data=Request::getRequest(); $result=Response::sendResponse($data); echo $result; ?> Request.php <?php class Request{ private static $method_type=array('get','post','put','delete','patch'); private static $FruitList=array( 1=>array('name'=>'apple','num'=>10), 2=>array('name'=>'grape','num'=>20), 3=>array('name'=>'orange','num'=>40), 4=>array('name'=>'banana','num'=>30), 5=>array('name'=>'peach','num'=>50), 6=>array('name'=>'mango','num'=>60) ); public static function getRequest(){ $method=strtolower($_SERVER['REQUEST_METHOD']); if(in_array($method,self::$method_type)){ $methodName=$method.'Data'; return self::$methodName($_REQUEST); } return false; } //get 查询 private static function getData($params){ $fruitId=(int)$params['fruit']; if($fruitId>0){ return self::$FruitList[$fruitId]; }else{ return self::$FruitList; } } //post 添加 private static function postData($params){ if(!empty($params['name'])){ $data['name']=$params['name']; $data['num']=(int)$params['num']; self::$FruitList[]=$data; return self::$FruitList; }else{ return false; } } //put 更新水果信息(全部信息) private static function putData($params){ $fruitId=(int)$params['fruit']; if($fruitId==0){ return false; } $data=array(); if(!empty($params['name'] && isset($params['num']))){ $data['name']=$params['name']; $data['num']=(int)$params['num']; self::$FruitList['fruitId']=$data; return self::$FruitList; }else{ return false; } } //patch 更新水果部分信息 private static function patchData($params){ $fruitId=(int)$params['fruit']; if($fruitId==0){ return false; } if(!empty($params['name'])){ self::$FruitList[$fruitId]['name']=$params['name']; } if(!isset($params['num'])){ self::$FruitList[$fruitId]['num']=(int)$params['num']; } return self::$FruitList; } //delete 删除 private static function deleteData($params) { $fruitId = (int)$params['fruit']; if($fruitId==0){ return false; } unset(self::$FruitList[$fruitId]); return self::$FruitList; } } ?> Response.php <?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'; } //output 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){ return self::encodeJSON($data); }else if(strpos($content_type,'application/xml')!==false){ return self::encodeXml($data); }else{ return self::encodeHtml($data); } } //json private static function encodeJson($responseData){ header("Content-Type:application/json"); return json_encode($responseData); } //xml private static function encodeXml($params){ header("Content-Type:application/xml"); $xml=new SimpleXMLElement('<?xml version="1.0"?><fruit></fruit>'); foreach ($params 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){ header("Content-Type:text/html"); $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; } } ?>