首页  >  文章  >  后端开发  >  PHP如何实现RESTful原理

PHP如何实现RESTful原理

Guanhui
Guanhui原创
2020-05-14 17:45:572575浏览

PHP如何实现RESTful原理

PHP如何实现RESTful原理

首先定义一个请求数据接收类;然后请求类中根据请求类型,进行访问不通方法;

 class Request
  {
      // 允许的请求方式
      private static $method_type = array('get', 'post', 'put', 'patch', 'delete');

      // 测试数据
      private static $test_class = array(
          1 => array('name'=>'测试一班','count'=>18), 
          2 => array('name'=>'测试二班','count'=>15)
      );

      public static function getRequest()
      {
          // 请求方法
          $method = strtolower($_SERVER['REQUEST_METHOD']);
          if (in_array($method, self::$method_type)) {
              // 调用请求方法对应的方法
              $data_name = $method . "Data";
              return self::$data_name($_REQUEST);
          }
          return false;
      }

      // GET 获取信息
      private static function getData($request_data)
      {
          $class_id = (int)$request_data['class'];
          if ($class_id > 0) {
              // GET /class/ID: 获取某个指定班的信息
              return self::$test_class[$class_id];
          }else{ 
              // GET /class: 列出所有班级
              return self::$test_class;
          }
      }

      // POST /class 新建一个班级
      private static function postData($request_data)
      {
          $class_id = (int)$request_data['class'];
          if ($class_id == 0) {
              return false;
          }
          $data = array();
          if (!empty($request_data['name']) && isset($request_data['count'])) {
              $data['name'] = $request_data['name'];
              $data['count'] = $request_data['count'];
              self::$test_class[] = $data;
              return self::$test_class; 
          }else{
              return false;
          }
      }

      // PUT /class/ID 更新某个指定班级的信息(全部信息)
      private static function putData($request_data)
      {
          $class_id = (int)$request_data['class'];
          if ($class_id == 0) {
              return false;
          }

          $data = array();
          if (!empty($request_data['name']) && isset($request_data['count'])) {
              $data['name'] = $request_data['name'];
              $data['count'] = (int)$request_data['count'];
              self::$test_class[$class_id] = $data;
              return self::$test_class;
          }else{
              return false;
          }
      }

      // PATCH /class/ID 更新某个指定班级的信息 (部分信息)
      private static function pacthData($request_data)
      {
          $class_id = (int)$request_data['class'];
          if ($class_id == 0) {
              return false;
          }
          if (!empty($request_data['name'])) {
              self::$test_class[$class_id]['name'] = $request_data['name'];
          }
          if (isset($request_data['count'])) {
              self::$test_class[$class_id]['count'] = $request_data['count'];
          }
          return self::$test_class;
      }

      // DELETE /class/ID 删除某个班
      private static function deleteData($request_data)
      {
          $class_id = (int)$request_data['class'];
          if ($class_id == 0) {
              return false;
          }
          unset(self::$test_class[$class_id]);
          return self::$test_class;
      }
  }

再定义一个数据输出类,将数据输出的格式进行统一的封装;最后将方法返回的数据进行输出即可。

  <?php
  /**
  * 包含一个Response类,即输出类。根据接收到的Content-Type,将Request类返回的数组拼接成对应的格式,加上header后输出
  */
  class Response
  {
      const HTTP_VERSION = "HTTP/1.1";
  
      public function sendResponse($data)
      {
          // 获取数据
          if ($data) {
              $code = 200;
              $message = "OK";
          }else{
              $code = 404;
              $data = array(&#39;error&#39; => "Not Found");
              $message = "Not Found";
          }
  
          header(self::HTTP_VERSION . " $code $message");
          $content_type = isset($_SERVER[&#39;CONTENT_TYPE&#39;]) ? $_SERVER[&#39;CONTENT_TYPE&#39;] : $_SERVER[&#39;HTTP_ACCEPT&#39;];
          if (strpos($content_type, &#39;application/json&#39;) !== false) {
              header("Content-Type: application/json");
              echo self::encodeJson($data);
          }elseif (strpos($content_type, &#39;application/xml&#39;) !== 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(&#39;<?xml version="1.0"?><rest></rest>&#39;);
          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=&#39;1&#39;>";
          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;
      }
  }
  ?>

推荐教程:《PHP教程

以上是PHP如何实现RESTful原理的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关文章

查看更多