使用PHP创建一个REST API(Create a REST API with PHP) 节选3

6 篇文章 0 订阅
5 篇文章 0 订阅
Getting Started with REST and PHP 开始使用PHP写REST


One last disclaimer: the code we’re about to go over is in no way intended to be used as an example of a robust solution. My main goal here is to show how to deal with the individual components of REST in PHP, and leave creating the final solution up to you. 
最后一项免责声明:我们接下来提供的代码并不能被用来作为一个稳健的解决方案。我的主要目的是向大家展示如果使用PHP处理REST的每个单独部分,而把最后的解决方案留给你们自己去创建。


So, let’s dig in! I think the best way to do something practical is to create a class that will provide all the utility functions we need to create a REST API. We’ll also create a small class for storing our data. You could also then take this, extend it, and apply it to your own needs. So, let’s stub some stuff out: 
那么,让我们开始深入代码。我认为做一个实际事情做好的方法就是新建一个class,这个class将提供创建REST API所需要的所有功能性方法。现在我们新建一个小的class来存储我们的数据。你可以把它拿去扩展一下然后应用到自己的需求中。我们现在开始写点东西:

PHP代码 
  1. class RestUtils
  2. {
  3.   public static function processRequest(){
  4.   }
  5.   public static function sendResponse($status = 200, $body = ''$content_type = 'text/html'){
  6.   }
  7.   public static function getStatusCodeMessage($status){
  8.     // these could be stored in a .ini file and loaded
  9.     // via parse_ini_file()... however, this will suffice
  10.     // for an example
  11.     // 这些应该被存储在一个.ini的文件中,然后通过parse_ini_file()函数来解析出来,然而这样也足够了,比如:
  12.     $codes = Array(
  13.       100 => 'Continue',
  14.       101 => 'Switching Protocols',
  15.       200 => 'OK',
  16.       201 => 'Created',
  17.       202 => 'Accepted',
  18.       203 => 'Non-Authoritative Information',
  19.       204 => 'No Content',
  20.       205 => 'Reset Content',
  21.       206 => 'Partial Content',
  22.       300 => 'Multiple Choices',
  23.       301 => 'Moved Permanently',
  24.       302 => 'Found',
  25.       303 => 'See Other',
  26.       304 => 'Not Modified',
  27.       305 => 'Use Proxy',
  28.       306 => '(Unused)',
  29.       307 => 'Temporary Redirect',
  30.       400 => 'Bad Request',
  31.       401 => 'Unauthorized',
  32.       402 => 'Payment Required',
  33.       403 => 'Forbidden',
  34.       404 => 'Not Found',
  35.       405 => 'Method Not Allowed',
  36.       406 => 'Not Acceptable',
  37.       407 => 'Proxy Authentication Required',
  38.       408 => 'Request Timeout',
  39.       409 => 'Conflict',
  40.       410 => 'Gone',
  41.       411 => 'Length Required',
  42.       412 => 'Precondition Failed',
  43.       413 => 'Request Entity Too Large',
  44.       414 => 'Request-URI Too Long',
  45.       415 => 'Unsupported Media Type',
  46.       416 => 'Requested Range Not Satisfiable',
  47.       417 => 'Expectation Failed',
  48.       500 => 'Internal Server Error',
  49.       501 => 'Not Implemented',
  50.       502 => 'Bad Gateway',
  51.       503 => 'Service Unavailable',
  52.       504 => 'Gateway Timeout',
  53.       505 => 'HTTP Version Not Supported'
  54.     );
  55.     return (isset($codes[$status])) ? $codes[$status] : '';
  56.   }
  57. }
  58. class RestRequest
  59. {
  60.   private $request_vars;
  61.   private $data;
  62.   private $http_accept;
  63.   private $method;
  64.   public function __construct(){
  65.     $this->request_vars    = array();
  66.     $this->data        = '';
  67.     $this->http_accept    = (strpos($_SERVER['HTTP_ACCEPT'], 'json')) ? 'json' : 'xml';
  68.     $this->method      = 'get';
  69.   }
  70.   public function setData($data){
  71.     $this->data = $data;
  72.   }
  73.   public function setMethod($method){
  74.     $this->method = $method;
  75.   }
  76.   public function setRequestVars($request_vars){
  77.     $this->request_vars = $request_vars;
  78.   }
  79.   public function getData(){
  80.     return $this->data;
  81.   }
  82.   public function getMethod(){
  83.     return $this->method;
  84.   }
  85.   public function getHttpAccept(){
  86.     return $this->http_accept;
  87.   }
  88.   public function getRequestVars(){
  89.     return $this->request_vars;
  90.   }
  91. }
OK, so what we’ve got is a simple class for storing some information about our request (RestRequest), and a class with some static functions we can use to deal with requests and responses. As you can see, we really only have two functions to write… which is the beauty of this whole thing! Right, let’s move on… 
Processing the Request 
好,现在我们有了一个简单的class来存储request的一些信息(RestRequest),和一个提供几个静态方法的class来处理请求和响应。就像你能看到的,我们还有两个方法要去写,这才是整个代码的关键所在,让我们继续...


Processing the request is pretty straight-forward, but this is where we can run into a few catches (namely with PUT and DELETE… mostly PUT). We’ll go over those in a moment, but let’s examine the RestRequest class a bit. If you’ll look at the constructor, you’ll see that we’re already interpreting the HTTP_ACCEPT header, and defaulting to JSON if none is provided. With that out of the way, we need only deal with the incoming data. 

处理请求的过程非常直接,但是这才是我们可以有所收获的地方(即PUT/DELETE,大多数是PUT),我们接下来将会讨论这些。但是让我们先来检查一下RestRequest这个class,在构造方法中,你会看到我们已经处理了HTTP_ACCEPT的头信息,并且将JSON作为默认值。这样,我们就只需要处理传入的数据。


目录:

使用PHP创建一个REST API(Createa REST API with PHP) 节选1

使用PHP创建一个REST API(Createa REST API with PHP) 节选2

使用PHP创建一个REST API(Createa REST API with PHP) 节选3

使用PHP创建一个REST API(Create aREST API with PHP) 节选4

使用PHP创建一个REST API(Createa REST API with PHP) 节选5

使用PHP创建一个REST API(Createa REST API with PHP) 节选6



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值