博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP 简例 RestFul
阅读量:7192 次
发布时间:2019-06-29

本文共 6165 字,大约阅读时间需要 20 分钟。

hot3.png

这里简单介绍个接口实例。

以下代码是 RESTful 服务类 Site.php:

实例

'TaoBao', 2 => 'Google', 3 => 'Runoob', 4 => 'Baidu', 5 => 'Weibo', 6 => 'Sina' ); public function getAllSite(){ return $this->sites; } public function getSite($id){ $site = array($id => ($this->sites[$id]) ? $this->sites[$id] : $this->sites[1]); return $site; } }?>

RESTful Services URI 映射

RESTful Services URI 应该设置为一个直观简短的资源地址。Apache 服务器的 .htaccess 应设置好对应的 Rewrite 规则。
本实例我们将使用两个 URI 规则:
1、获取所有站点列表:

http://localhost/restexample/site/list/

2、使用 id 获取指定的站点,以下 URI 为获取 id 为 3 的站点:

http://localhost/restexample/site/list/3/

项目的 .htaccess 文件配置规则如下所示:

# 开启 rewrite 功能Options +FollowSymlinksRewriteEngine on# 重写规则RewriteRule ^site/list/$   RestController.php?view=all [nc,qsa]RewriteRule ^site/list/([0-9]+)/$   RestController.php?view=single&id=$1 [nc,qsa]

RESTful Web Service 控制器

在 .htaccess 文件中,我们通过设置参数 'view' 来获取 RestController.php 文件中对应的请求,通过获取 'view' 不同的参数来分发到不同的方法上。RestController.php 文件代码如下:
实例

getAllSites(); break; case "single": // 处理 REST Url /site/show/
/ $siteRestHandler = new SiteRestHandler(); $siteRestHandler->getSite($_GET["id"]); break; case "" : //404 - not found; break;}?>

简单的 RESTful 基础类

以下提供了 RESTful 的一个基类,用于处理响应请求的 HTTP 状态码,SimpleRest.php 文件代码如下:
实例

getHttpStatusMessage($statusCode); header($this->httpVersion. " ". $statusCode ." ". $statusMessage); header("Content-Type:". $contentType); } public function getHttpStatusMessage($statusCode){ $httpStatus = array( 100 => 'Continue', 101 => 'Switching Protocols', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => '(Unused)', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported'); return ($httpStatus[$statusCode]) ? $httpStatus[$statusCode] : $status[500]; }}?>

RESTful Web Service 处理类

以下是一个 RESTful Web Service 处理类 SiteRestHandler.php,继承了上面我们提供的 RESTful 基类,类中通过判断请求的参数来决定返回的 HTTP 状态码及数据格式,实例中我们提供了三种数据格式: "application/json" 、 "application/xml" 或 "text/html":
SiteRestHandler.php 文件代码如下:
实例

getAllSite(); if(empty($rawData)) { $statusCode = 404; $rawData = array('error' => 'No sites found!'); } else { $statusCode = 200; } $requestContentType = $_SERVER['HTTP_ACCEPT']; $this ->setHttpHeaders($requestContentType, $statusCode); if(strpos($requestContentType,'application/json') !== false){ $response = $this->encodeJson($rawData); echo $response; } else if(strpos($requestContentType,'text/html') !== false){ $response = $this->encodeHtml($rawData); echo $response; } else if(strpos($requestContentType,'application/xml') !== false){ $response = $this->encodeXml($rawData); echo $response; } } public function encodeHtml($responseData) { $htmlResponse = "
"; foreach($responseData as $key=>$value) { $htmlResponse .= "
"; } $htmlResponse .= "
". $key. " ". $value. "
"; return $htmlResponse; } public function encodeJson($responseData) { $jsonResponse = json_encode($responseData); return $jsonResponse; } public function encodeXml($responseData) { // 创建 SimpleXMLElement 对象 $xml = new SimpleXMLElement('
'); foreach($responseData as $key=>$value) { $xml->addChild($key, $value); } return $xml->asXML(); } public function getSite($id) { $site = new Site(); $rawData = $site->getSite($id); if(empty($rawData)) { $statusCode = 404; $rawData = array('error' => 'No sites found!'); } else { $statusCode = 200; } $requestContentType = $_SERVER['HTTP_ACCEPT']; $this ->setHttpHeaders($requestContentType, $statusCode); if(strpos($requestContentType,'application/json') !== false){ $response = $this->encodeJson($rawData); echo $response; } else if(strpos($requestContentType,'text/html') !== false){ $response = $this->encodeHtml($rawData); echo $response; } else if(strpos($requestContentType,'application/xml') !== false){ $response = $this->encodeXml($rawData); echo $response; } }}?>

最后我们这样访问我们写的接口

http://localhost/site/list/ http://localhost/site/list/1/

本文代码来自 

转载于:https://my.oschina.net/slagga/blog/2874340

你可能感兴趣的文章
第9章 保护Web应用
查看>>
IO多路复用之select
查看>>
餐厅定位:编写一个程序,访问用户有多少人用餐。如果超过8个,就打印一条消息指出没有空桌,否则指出有空桌。...
查看>>
partition-list
查看>>
4.2. 入门案例
查看>>
Laravel资源理由器跟隐式控制的对比及是怎样的吧?- Route::resource vs Route::controller...
查看>>
mysql数据库分区功能及实例详解
查看>>
点击 小眼睛,和 i,
查看>>
OVN 问题小记
查看>>
mysql
查看>>
从hive将数据导出到mysql(转)
查看>>
写给来到这里的每一位
查看>>
dialog 中装listview并让每一个item分隔悬空,并具有radiobutton的效果
查看>>
ASP.NET中插入FLASH代码
查看>>
通过jquery 获取文本框的聚焦和失焦方法
查看>>
7 JavaScript Basics Many Developers Aren't Using (Properly)【转】
查看>>
Eclipse之JSP页面的使用
查看>>
Python入门篇-函数、参数及参数解构
查看>>
Android上获取本机安装的应用程序
查看>>
Android 手势识别
查看>>