首页 > 代码库 > 使用TP5创建一个REST API

使用TP5创建一个REST API

原文在这里 : http://hmw.iteye.com/blog/1190827

 

tp自带的api,get请求接口

/**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
//        $request_method = strtolower($_SERVER[‘REQUEST_METHOD‘]);
//        $return_onj = new RestRequest();
//        $data = http://www.mamicode.com/db(‘ShopGoods‘)->select();>

 

 

 

2、  实现接口请求处理和请求处理的方法‘’

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/26
 * Time: 10:18
 */
namespace app\api\controller;
use app\api\controller\RestRequest;

//简单的class来存储request的一些信息(RestRequest),和一个提供几个静态方法的class来处理请求和响应
class RestUtils
{
    //根据请求的内容类型来处理JSON或者XML,但是让我们现在简单一点。那么,我们处理请求的方法将会类似于这样
    public static function processRequest(){
        // get our verb 获取动作
        $request_method = strtolower($_SERVER[‘REQUEST_METHOD‘]);
        $return_obj     = new RestRequest();
        // we‘ll store our data here 在这里存储请求数据
        $data           = http://www.mamicode.com/array();"ServerSignature On")
            $signature = ($_SERVER[‘SERVER_SIGNATURE‘] == ‘‘) ? $_SERVER[‘SERVER_SOFTWARE‘] . ‘ Server at ‘ . $_SERVER[‘SERVER_NAME‘] . ‘ Port ‘ . $_SERVER[‘SERVER_PORT‘] : $_SERVER[‘SERVER_SIGNATURE‘];

            // this should be templatized in a real-world solution
            $body = ‘<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">  
                    <html>  
                        <head>  
                            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
                            <title>‘ . $status . ‘ ‘ . RestUtils::getStatusCodeMessage($status) . ‘</title>  
                        </head>  
                        <body>  
                            <h1>‘ . RestUtils::getStatusCodeMessage($status) . ‘</h1>  
                            ‘ . $message . ‘  
  
                            <hr />  
                            <address>‘ . $signature . ‘</address>  
                        </body>  
                    </html>‘;

            echo $body;
            exit;
        }
    }

    public static function getStatusCodeMessage($status){
        // these could be stored in a .ini file and loaded
        // via parse_ini_file()... however, this will suffice
        // for an example
        // 这些应该被存储在一个.ini的文件中,然后通过parse_ini_file()函数来解析出来,然而这样也足够了,比如:
        $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] : ‘‘;
    }
}

  

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/26
 * Time: 10:18
 */
namespace app\api\controller;
use app\api\controller\RestUtils;

//简单的class来存储request的一些信息(RestRequest),和一个提供几个静态方法的class来处理请求和响应

class RestRequest
{
    private $request_vars;
    private $data;
    private $http_accept;
    private $method;

    public function __construct()
    {
        $this->request_vars = array();
        $this->data = http://www.mamicode.com/‘‘;>

  

使用TP5创建一个REST API