Xem mẫu

Developing an API Let`s go with REST REST is now a very popular API architecture, with most social networks providing REST-based APIs. The way RESTful APIs rely on descriptive URIs for providing access to data makes them very easy for consumers to utilize, as the URIs provide information on what the request does, and what data it will return. Some implementations, such as the Twitter API even make it possible to change the format of the data returned, simply by changing a part of the URI. Requests Requests to a RESTful API use HTTP verbs to describe what the consumer is trying to do. The API requests are made to specific URIs, which define the resource that the consumer is trying to perform the action (determined by the verbs) upon. HTTP verbs The HTTP verbs and their usage are described as follows: Verb Description GET Retrieve information POST Create records PUT Update records DELETE Delete records Resources RESTful APIs relate URIs to resources. Below are some examples: • http://ourdomain.com/profiles: To list or create profiles • http://ourdomain.com/profiles/1: A specific user`s profile Our RESTful API will be based within an API controller, thus prefixing all URLs with api/, which goes slightly against the REST concept of a resource. [ 352 ] Download from www.eBookTM.com Chapter 11 Resources and verbs—the requests Let`s look at how resources and verbs combined result in API requests. API operation Creating a user Listing users Viewing a user`s profile Updating a profile Deleting a profile HTTP verb POST GET GET PUT DELETE Resource http://ourdomain.com/api/profiles http://ourdomain.com/api/profiles http://ourdomain.com/api/profiles/1 http://ourdomain.com/api/profiles/1 http://ourdomain.com/api/profiles/1 In the above resources, the number 1 represents the ID of a user`s profile. Responses The response to an API request is generally made up of two parts. The first part of the response is the HTTP header containing an appropriate status code. Some examples of HTTP status codes are below: HTTP status code 200 201 400 404 Meaning OK Created Bad request Not found Within PHP, HTTP status codes are set as follows: header("HTTP/1.0 404 Not Found"); The second part of the response is the data itself; for instance, if the API request was for a list of users, the response would be the list of users. Commonly, response data is sent as XML or JSON. Some APIs allow the consumer to request the format of the response by supplying the format to the API. We are going to use JSON. If we have an array of data that we want to return as JSON, we simply do the following: echo json_encode( $users_list_array ); exit(); [ 353 ] Download from www.eBookTM.com Developing an API Further reading There are numerous resources available regarding web services and REST. Following are the resources you may find particularly useful. RESTful PHP Web Services Packt has a book dedicated to creating RESTful APIs in PHP—RESTful PHP Web Services, by Samisa Abeysinghe, https://www.packtpub.com/restful-php-web-services/book. This book details the concepts of a REST architecture, how to make use of existing RESTful APIs in your framework, how to create a RESTful API for other applications to interact with, as well as debugging information and case studies. Conference talks Lorna Jane Mitchell (http://www.lornajane.net), a widely-respected developer and conference speaker on PHP-related topics, has recently spoken at a number of conferences on the subject of web service design. Slides from related talks are available online: http://www.slideshare.net/lornajane/best-practices-in-web-service-design, http://www.slideshare.net/lornajane/php-and-web-services-perfect-partners. Implementation Now that we know what sort of API we are going to develop, we can move onto the implementation. In this chapter we will only implement a small sub-set of the API`s functionality. Feel free to extend this to match the entire functionality of Dino Space, if you wish. Data format Most commonly, RESTful APIs either return their data in XML format or as JSON. Some APIs allow the consumer to specify the return type by adding .xml or .json to the end of the URL. For the purposes of our implementation, let`s stick to JSON, as it is simpler to convert data to JSON (simply by passing the data to the json_encode function). [ 354 ] Download from www.eBookTM.com Chapter 11 API controller Our API controller itself won`t do very much; instead it will pass control to delegate controllers, which contain logic specific to the various sections of the site. registry = $registry; $apiController = $registry->getObject(`url`)->getURLBit(1); $this->delegateControl( $apiController ); } [ 355 ] Download from www.eBookTM.com Developing an API The delegateControl method checks that the delegate controller is within the allowed delegates. If it is, then it includes the appropriate controller, instantiates it, and passes the registry and the API controller object to it. There are a number of methods that will be common to all API delegates. These methods are stored in this object, and called by the delegate referencing this object. If the requested controller is not allowable, then we generate an appropriate HTTP status code; in this case: 404 Not Found. /** * Pass control to a delegate * @param String $apiController the delegate * @return void */ private function delegateControl( $apiController ) { if( $apiController != `` && in_array( $apiController, $this->allowableAPIControllers ) ) { require_once( FRAMEWORK_PATH . `controllers/api/` . $apiController . `.php` ); $api = new APIDelegate( $this->registry, $this ); } else { header(`HTTP/1.0 404 Not Found`); exit(); } } A shared method is required by our delegates. This is called if a delegate requires the API user to be an authenticated user on the site. It generates a basic authentication prompt (this is presented to users viewing the site in their browsers, but for API users the username and password are passed as part of the HTTP request). [ 356 ] Download from www.eBookTM.com ... - tailieumienphi.vn
nguon tai.lieu . vn