《PHP編程 RESTful》要點(diǎn):
本文介紹了PHP編程 RESTful,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
歡迎參與《PHP編程 RESTful》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
REST(英文:Representational State Transfer,簡(jiǎn)稱REST) ,指的是一組架構(gòu)約束條件和原則.
符合REST設(shè)計(jì)風(fēng)格的Web API稱為RESTful API.它從以下三個(gè)方面資源進(jìn)行定義:
直觀簡(jiǎn)短的資源地址:URI,比如:http://example.com/resources/
.
傳輸?shù)馁Y源:Web服務(wù)接受與返回的互聯(lián)網(wǎng)媒體類型,比如:JSON,XML,YAM等.
對(duì)資源的操作:Web服務(wù)在該資源上所支持的一系列哀求方法(比如:POST,GET,PUT或DELETE).
本教程我們將使用 PHP(不用框架) 來(lái)創(chuàng)建一個(gè) RESTful web service,在文章末尾你可以下載本章節(jié)使用到的代碼.
通過(guò)本教程你將學(xué)習(xí)到以下內(nèi)容:
創(chuàng)建一個(gè) RESTful Webservice.
使用原生 PHP, 不依賴任何框架.
URI 模式需要遵循 REST 規(guī)則.
RESTful service 接受與返回的格式可以是 JSON, XML等.
根據(jù)不同情況響應(yīng)對(duì)應(yīng)的 HTTP 狀態(tài)碼.
演示哀求頭的使用.
使用 REST 客戶端來(lái)測(cè)試 RESTful web service.
RESTful Webservice 實(shí)例
以下代碼是 RESTful 服務(wù)類 Site.php:
實(shí)例
<?php/* * 菜鳥(niǎo)教程 RESTful 演示實(shí)例 * RESTful 服務(wù)類 */ClassSite{private$sites = array(1 => 'TaoBao', 2 => 'Google', 3 => 'Runoob', 4 => 'Baidu', 5 => 'Weibo', 6 => 'Sina'); publicfunctiongetAllSite(){return$this->sites; }publicfunctiongetSite($id){$site = array($id => ($this->sites[$id]) ? $this->sites[$id] : $this->sites[1]); return$site; }}?>
RESTful Services URI 映射
RESTful Services URI 應(yīng)該設(shè)置為一個(gè)直觀簡(jiǎn)短的資源地址.Apache 服務(wù)器的 .htaccess 應(yīng)設(shè)置好對(duì)應(yīng)的 Rewrite 規(guī)則.
本實(shí)例我們將使用兩個(gè) URI 規(guī)則:
1、獲取所有站點(diǎn)列表:
http://localhost/restexample/site/list/
2、使用 id 獲取指定的站點(diǎn),以下 URI 為獲取 id 為 3 的站點(diǎn):
http://localhost/restexample/site/list/3/
項(xiàng)目的 .htaccess 文件配置規(guī)則如下所示:
# 開(kāi)啟 rewrite 功能Options +FollowSymlinksRewriteEngine on# 重寫(xiě)規(guī)則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 文件中,我們通過(guò)設(shè)置參數(shù) 'view' 來(lái)獲取 RestController.php 文件中對(duì)應(yīng)的哀求,通過(guò)獲取 'view' 不同的參數(shù)來(lái)分發(fā)到不同的方法上.RestController.php 文件代碼如下:
實(shí)例
<?phprequire_once("SiteRestHandler.php"); $view = "";if(isset($_GET["view"]))$view = $_GET["view"];/* * RESTful service 控制器 * URL 映射*/switch($view){case"all": // 處理 REST Url /site/list/$siteRestHandler = newSiteRestHandler(); $siteRestHandler->getAllSites(); break; case"single": // 處理 REST Url /site/show/<id>/$siteRestHandler = newSiteRestHandler(); $siteRestHandler->getSite($_GET["id"]); break; case"" : //404 - not found;break;}?>
簡(jiǎn)單的 RESTful 基礎(chǔ)類
以下提供了 RESTful 的一個(gè)基類,用于處理響應(yīng)哀求的 HTTP 狀態(tài)碼,SimpleRest.php 文件代碼如下:
實(shí)例
<?php/* * 一個(gè)簡(jiǎn)單的 RESTful web services 基類 * 我們可以基于這個(gè)類來(lái)擴(kuò)展需求*/classSimpleRest{private$httpVersion = "HTTP/1.1"; publicfunctionsetHttpHeaders($contentType, $statusCode){$statusMessage = $this -> getHttpStatusMessage($statusCode); header($this->httpVersion. "". $statusCode ."". $statusMessage); header("Content-Type:". $contentType); }publicfunctiongetHttpStatusMessage($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 處理類
以下是一個(gè) RESTful Web Service 處理類 SiteRestHandler.php,繼承了上面我們提供的 RESTful 基類,類中通過(guò)判斷哀求的參數(shù)來(lái)決定返回的 HTTP 狀態(tài)碼及數(shù)據(jù)格式,實(shí)例中我們提供了三種數(shù)據(jù)格式: "application/json" 、 "application/xml" 或 "text/html":
SiteRestHandler.php 文件代碼如下:
實(shí)例
<?phprequire_once("SimpleRest.php");require_once("Site.php"); classSiteRestHandlerextendsSimpleRest{functiongetAllSites(){$site = newSite(); $rawData = $site->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; }elseif(strpos($requestContentType,'text/html') !== false){$response = $this->encodeHtml($rawData); echo$response; }elseif(strpos($requestContentType,'application/xml') !== false){$response = $this->encodeXml($rawData); echo$response; }}publicfunctionencodeHtml($responseData){$htmlResponse = "<table border='1'>"; foreach($responseDataas$key=>$value){$htmlResponse .= "<tr><td>". $key. "</td><td>". $value. "</td></tr>"; }$htmlResponse .= "</table>"; return$htmlResponse; }publicfunctionencodeJson($responseData){$jsonResponse = json_encode($responseData); return$jsonResponse; }publicfunctionencodeXml($responseData){// 創(chuàng)建 SimpleXMLElement 對(duì)象$xml = newSimpleXMLElement('<?xml version="1.0"?><site></site>'); foreach($responseDataas$key=>$value){$xml->addChild($key, $value); }return$xml->asXML(); }publicfunctiongetSite($id){$site = newSite(); $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; }elseif(strpos($requestContentType,'text/html') !== false){$response = $this->encodeHtml($rawData); echo$response; }elseif(strpos($requestContentType,'application/xml') !== false){$response = $this->encodeXml($rawData); echo$response; }}}?>
接下來(lái)我們通過(guò) http://localhost/restexample/site/list/ 訪問(wèn),輸出結(jié)果如下:
RESTful Web Service 客戶端
接下來(lái)我們可以使用 Google Chrome 瀏覽器的 "Advance Rest Client" 作為 RESTful Web Service 客戶端來(lái)哀求我們的服務(wù).
實(shí)例中哀求 http://localhost/restexample/site/list/ 地址,接收數(shù)據(jù)類似為 Accept: application/json
哀求 id 為 3 的站點(diǎn) Runoob(菜鳥(niǎo)教程),訪問(wèn)地址為 http://localhost/restexample/site/list/3/,
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/12405.html