• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php创建简单的RestfulWebAPI(三)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  上篇记录了怎样实现route,本篇记录怎么实现request,response。

  Request 处理请求

<?php

class Request
{
    private $request_vars;  
    private $data;  
    private $http_accept;  
    private $method; 
    private $ID; 
  
    public function __construct($id = null)  
    {  
        $this->request_vars      = array();  
        $this->data              = array(); 
        $this->http_accept       = 'application/json';  
        $this->method            = 'get';  
        $this->ID                = $id;  
        $this->processRequest();
    }  

    public function processRequest()
    {
        $request_method = strtolower($_SERVER['REQUEST_METHOD']);
        $this->setMethod($request_method);  

        $request_vars = $_GET;
        if($request_vars != ''){
            $this->setRequestVars($request_vars); 
        }  

        $data = file_get_contents('php://input');
        if($data != ''){
            $this->setData(json_decode($data,TRUE));  
        } 
    } 

    public function setID($id)  
    {  
        $this->ID = $id;  
    }  
  
    public function setData($data)  
    {  
        $this->data = $data;  
    }  
  
    public function setMethod($method)  
    {  
        $this->method = $method;  
    }  
  
    public function setRequestVars($request_vars)  
    {  
        $this->request_vars = $request_vars;  
    }

    public function getID()  
    {  
        return $this->ID;  
    }  
  
    public function getData()  
    {  
        return $this->data;  
    }  
  
    public function getMethod()  
    {  
        return $this->method;  
    }  
  
    public function getHttpAccept()  
    {  
        return $this->http_accept;  
    }  
  
    public function getRequestVars()  
    {  
        $this->request_vars['id'] = $this->ID;
        return $this->request_vars;  
    }  
}  

?>

  代码十分简单,就是从request获取请求method,querystring,请求body数据(json格式)。

  Response 发送响应

<?php

class Response
{
    public function __construct()  
    {
    }

    public function sendResponse($status = 200, $body = '', $content_type = 'application/json') 
    {
         $status_header = 'HTTP/1.1 ' . $status . ' ' . $this->getStatusCodeMessage($status);  
         header($status_header);  
         header('Content-type: ' . $content_type . '; charset=utf-8'); 
         echo $body;
         exit;  
    }

    public function getStatusCodeMessage($status)  
    {  
        $codes = 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 (isset($codes[$status])) ? $codes[$status] : '';  
    }
}  

?>

  生成response。

  这两个类为工具类,实现也不复杂,功能单一,需要继续丰富修改。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
redis扩展CentOS7安装Redis和PHP-redis扩展发布时间:2022-07-10
下一篇:
PHP密码重置,发送邮件,随机长度字母数字密码发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap