zoukankan      html  css  js  c++  java
  • PHP实现RESTful风格的API实例(二)

    接前一篇:PHP实现RESTful风格的API实例(一)

    Response.php :包含一个Request类,即输出类。根据接收到的Content-Type,将Request类返回的数组拼接成对应的格式,加上header后输出

     
    <?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';
            }
    
            //输出结果
            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) {
                header("Content-Type: application/json");
                echo self::encodeJson($data);
            } else if (strpos($content_type, 'application/xml') !== false) {
                header("Content-Type: application/xml");
                echo self::encodeXml($data);
            } else {
                header("Content-Type: text/html");
                echo self::encodeHtml($data);
            }
        }
    
        //json格式
        private static function encodeJson($responseData)
        {
            return json_encode($responseData);
        }
    
        //xml格式
        private static function encodeXml($responseData)
        {
            $xml = new SimpleXMLElement('<?xml version="1.0"?><rest></rest>');
            foreach ($responseData 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)
        {
            $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;
        }
    }
     

    index.php :入口文件,调用Request类取得数据后交给Response处理,最后返回结果

     
    <?php
    //数据操作类
    require('Request.php');
    //输出类
    require('Response.php');
    //获取数据
    $data = Request::getRequest();
    //输出结果
    Response::sendResponse($data);
     

    下一篇:PHP实现RESTful风格的API实例(三)

  • 相关阅读:
    MP4文件格式
    ASP.net 学习路线(详细)
    H.264开源解码器评测
    H264编码 封装成MP4格式 视频流 RTP封包
    MP4介绍与基本AVC编码(x264)教程
    创建一个简单的WCF程序
    VUE 从零开始 学习笔记 一
    关于阿里云短信接口的使用
    对Ul下的li标签执行点击事件——如何获取你所点击的标签
    二逼程序员与苦逼程序员
  • 原文地址:https://www.cnblogs.com/quanzhiguo/p/7157773.html
Copyright © 2011-2022 走看看