您身边的网站建设专家
成功案例

【建站服务】平凉公众号代运营公司-域名申请

日期: 2022-09-21 03:05:10 浏览数:3


​

上往建站提供服务器空间服务商,百度快照排名,网站托管,百度推广运营,致力于设计外包服务与源代码定制开发,360推广,搜狗推广,增加网站的能见度及访问量提升网络营销的效果,主营:网站公司,百度推广公司电话,官网搭建服务,网站服务企业排名,服务器空间,英文域名等业务,专业团队服务,效果好。


平凉公众号代运营公司

网站建设.png

ESTful 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 文件代码如下:

实例

<?phprequire_once("SiteRestHandler.php");        $view = "";if(isset($_GET["view"]))    $view = $_GET["view"];/* * RESTful service 控制器 * URL 映射*/switch($view){    case "all":        // 处理 REST Url /site/list/        $siteRestHandler = new SiteRestHandler();        $siteRestHandler->getAllSites();        break;            case "single":        // 处理 REST Url /site/show/<id>/        $siteRestHandler = new SiteRestHandler();        $siteRestHandler->getSite($_GET["id"]);        break;    case "" :        //404 - not found;        break;}?>

简单的 RESTful 基础类

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

实例

<?php /* * 一个简单的 RESTful web services 基类 * 我们可以基于这个类来扩展需求*/class SimpleRest {        private $httpVersion = "HTTP/1.1";    public function setHttpHeaders($contentType, $statusCode){                $statusMessage = $this -> 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 文件代码如下:

实例

<?php require_once("SimpleRest.php");require_once("Site.php"); class SiteRestHandler extends SimpleRest {    function getAllSites() {            $site = new Site();        $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;        } else if(strpos($requestContentType,'text/html') !== false){            $response = $this->encodeHtml($rawData);            echo $response;        } else if(strpos($requestContentType,'application/xml') !== false){            $response = $this->encodeXml($rawData);



 

        $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 = "<table border='1'>";

        foreach($responseData as $key=>$value) {

                $htmlResponse .= "<tr><td>". $key. "</td><td>". $value. "</td></tr>";

        }

        $htmlResponse .= "</table>";

        return $htmlResponse;        

    }

    

    public function encodeJson($responseData) {

        $jsonResponse = json_encode($responseData);

        return $jsonResponse;        

    }

    

    public function encodeXml($responseData) {

        // 创建 SimpleXMLElement 对象

        $xml = new SimpleXMLElement('<?xml version="1.0"?><site></site>');

        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/restexample/site/list/ 访问,输出结果如下:



RESTful Web Service 客户端

接下来我们可以使用 Google Chrome 浏览器的 "Advance Rest Client" 作为 RESTful Web Service 客户端来请求我们的服务。


实例中请求 http://localhost/restexample/site/list/ 地址,接收数据类似为 Accept: application/json




请求 id 为 3 的站点 Runoob(菜鸟教程),访问地址为 http://localhost/restexample/site/list/3/,




源码下载

实例中使用到的代码可点击以下按钮下载:


源码下载


平凉公众号代运营公司


上往建站提供搭建网站,域名注册,官网备案服务,网店详情页设计,企业网店,专业网络店铺管理运营全托管公司咨询电话,服务器空间,微信公众号托管,网页美工排版,致力于域名申请,竞价托管,软文推广,全网营销,提供标准级专业技术保障,了却后顾之忧,主营:虚拟主机,网站推广,百度竞价托管,网站建设,上网建站推广服务,网络公司有哪些等业务,专业团队服务,效果好。

服务热线:400-111-6878 手机微信同号:18118153152(各城市商务人员可上门服务)


全国咨询热线:400-111-6878

地址:全国各地都有驻点商务

Copyright © 2021 通陆科技

网站建设:上往建站