您的位置:首页 > 编程语言 > PHP开发

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

2013-03-25 13:46 615 查看
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代码 

public static function processRequest(){

  // get our verb 获取动作

  $request_method = strtolower($_SERVER['REQUEST_METHOD']);

  $return_obj    = new RestRequest();

  // we'll store our data here 在这里存储请求数据

  $data      = array();

  switch ($request_method){

    // gets are easy...

    case 'get':

    $data = $_GET;

    break;

    // so are posts

    case 'post':

    $data = $_POST;

    break;

    // here's the tricky bit...

    case 'put':

    // basically, we read a string from PHP's special input location,

    // and then parse it out into an array via parse_str... per the PHP docs:

    // Parses str  as if it were the query string passed via a URL and sets

    // variables in the current scope.

    parse_str(file_get_contents('php://input'), $put_vars);

    $data = $put_vars;

    break;

  }

  // store the method

  $return_obj->setMethod($request_method);

  // set the raw data, so we can access it if needed (there may be

  // other pieces to your requests)

  $return_obj->setRequestVars($data);

  if(isset($data['data'])){

    // translate the JSON to an Object for use however you want

    $return_obj->setData(json_decode($data['data']));

  }

  return $return_obj;

}

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代码 

$data = RestUtils::processRequest();

switch($data->getMethod){

  case 'get':

    // retrieve a list of users

    break;

  case 'post':

    $user = new User();

    $user->setFirstName($data->getData()->first_name);  //
just for example, this should be done cleaner

    // and so on...

    $user->save();

    break;

  // etc, etc, etc...

}

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: