PHP如何实现RESTful原理-PHP问题

资源魔 81 0

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('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);
          }elseif (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;
      }
  }
  ?>

保举教程:《PHP教程》

以上就是PHP若何完成RESTful原理的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 api restful php故障解决 php使用问题

抱歉,评论功能暂时关闭!