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

6 篇文章 0 订阅
5 篇文章 0 订阅
There are a few ways we could go about doing this, but let’s just assume that we’ll always get a key/value pair in our request: ‘data’ => actual data. Let’s also assume that the actual data will be JSON. As stated in my previous explanation of REST, you could look at the content-type of the request and deal with either JSON or XML, but let’s keep it simple for now. So, our process request function will end up looking something like this: 
我们有几个方法可以选择,但是让我们假设在请求信息的总是可以接收到键/值对:'data'=>真实数据。同时假设真实数据是JSON格式的。正如我前文所述,你可以根据请求的内容类型来处理JSON或者XML,但是让我们现在简单一点。那么,我们处理请求的方法将会类似于这样:

PHP代码 
  1. public static function processRequest(){
  2.   // get our verb 获取动作
  3.   $request_method = strtolower($_SERVER['REQUEST_METHOD']);
  4.   $return_obj    = new RestRequest();
  5.   // we'll store our data here 在这里存储请求数据
  6.   $data      = array();
  7.   switch ($request_method){
  8.     // gets are easy...
  9.     case 'get':
  10.     $data = $_GET;
  11.     break;
  12.     // so are posts
  13.     case 'post':
  14.     $data = $_POST;
  15.     break;
  16.     // here's the tricky bit...
  17.     case 'put':
  18.     // basically, we read a string from PHP's special input location,
  19.     // and then parse it out into an array via parse_str... per the PHP docs:
  20.     // Parses str  as if it were the query string passed via a URL and sets
  21.     // variables in the current scope.
  22.     parse_str(file_get_contents('php://input'), $put_vars);
  23.     $data = $put_vars;
  24.     break;
  25.   }
  26.   // store the method
  27.   $return_obj->setMethod($request_method);
  28.   // set the raw data, so we can access it if needed (there may be
  29.   // other pieces to your requests)
  30.   $return_obj->setRequestVars($data);
  31.   if(isset($data['data'])){
  32.     // translate the JSON to an Object for use however you want
  33.     $return_obj->setData(json_decode($data['data']));
  34.   }
  35.   return $return_obj;
  36. }
Like I said, pretty straight-forward. However, a few things to note… First, you typically don’t accept data for DELETE requests, so we don’t have a case for them in the switch. Second, you’ll notice that we store both the request variables, and the parsed JSON data. This is useful as you may have other stuff as a part of your request (say an API key or something) that isn’t truly the data itself (like a new user’s name, email, etc.). 
正如我刚才所说的,非常的简单直接高效。然后,有几点需要注意:首先,我们不接受DELETE请求,因此我们在switch中不提供相应的case条件。其次,你会注意到我们把请求参数和解析后的JSON数据都存储起来了,这在请求中有其他需要处理的数据时会变得非常有用(API key或者其他),这些并不是请求的数据本身(比如一个新用户的名字、电子邮箱等)。


So, how would we use this? Let’s go back to the user example. Assuming you’ve routed your request to the correct controller for users, we could have some code like this: 
那么,我们如何使用它呢?让我们回到刚才user的例子。假设你已经通过路由把请求对应到正确的users控制器,代码如下:

PHP代码 
  1. $data = RestUtils::processRequest();
  2. switch($data->getMethod){
  3.   case 'get':
  4.     // retrieve a list of users
  5.     break;
  6.   case 'post':
  7.     $user = new User();
  8.     $user->setFirstName($data->getData()->first_name);  // just for example, this should be done cleaner
  9.     // and so on...
  10.     $user->save();
  11.     break;
  12.   // etc, etc, etc...
  13. }
Please don’t do this in a real app, this is just a quick-and-dirty example. You’d want to wrap this up in a nice control structure with everything abstracted properly, but this should help you get an idea of how to use this stuff. But I digress, let’s move on to sending a response. 
Sending the Response 

请不要在真实的应用中这样做,这是一个非常快速和不干净的示例。你应该使用一个设计良好的控制结构来把它包裹起来,适当的抽象化,但是这样有助于你理解如何使用这些东西。让我们继续代码,发送一个响应信息。

目录:

使用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、付费专栏及课程。

余额充值