Monday, December 29, 2014

Working with RESTful Services in CodeIgniter

Provide Multiple Representations for Resources

The “R” in REST stands for Representational. This means that REST services should provide different representations in order to serve a wide spectrum of clients.
Using HTTP content negotiation, a client can ask for a representation in a specific format. REST services should attempt to support the standard ones to encourage interoperability with many clients.

Use HTTP Status Codes for Responses

HTTP status codes provide a standard way for the server to inform the client about the status of the request.
  • 200 (OK) confirms the success of a GET, PUT, or DELETE request.
  • 201 (Created) confirms the success of a POST request.
  • 304 (Not Modified) is used by a conditional GET to inform the client that the resource has not been modified.
  • 400 (Bad Request) indicates a malformed request, often for a POST or PUT in which the request’s content is invalid.
  • 401 (Unauthorized) is used to indicate that authentication is required.
  • 403 (Forbidden) indicates that the client is not authorized for the requested action.
  • 404 (Not Found) is used to respond to any request to indicate that the resource could not be found.
  • 405 (Method Not Allowed) informs the client the that requested HTTP method is not available for that resource.
  • 409 (Conflict) should be used for situations where there is a conflict which prevents the service to perform the operation, but there is still a chance that the client might be able to resolve the conflict and resubmit the request.

  • xml - almost any programming language can read XML
  • json - useful for JavaScript and increasingly PHP apps.
  • csv - open with spreadsheet programs
  • html - a simple HTML table
  • php - Representation of PHP code that can be eval()'ed
  • serialize - Serialized data that can be unserialized in PHP
Used to fetch information about an existing resource. This is used by browsers when you enter a URL and hit go, or when you click on a link, so it perfect for fetching information on one of your REST resources (like user).
Used to update an existing resource with information. Browsers use this to submit most types of forms on the internet, although some use GET as well by submitting the form action with a query string containing the field data.
Less commonly used and not supported by most browsers, PUT is used to create a new resource.
Also not used by many browsers, this HTTP verb rather obviously is used to delete a resource.

<?php
require(APPPATH'.libraries/REST_Controller.php');
 
class Example_api extends REST_Controller {
 
    function user_get()
    {
        $data = array('returned: '. $this->get('id'));
        $this->response($data);
    }
     
    function user_post()
    {      
        $data = array('returned: '. $this->post('id'));
        $this->response($data);
    }
 
    function user_put()
    {      
        $data = array('returned: '. $this->put('id'));
        $this->response($data;
    }
 
    function user_delete()
    {
        $data = array('returned: '. $this->delete('id'));
        $this->response($data);
    }
}
 
Although the download comes with a full CodeIgniter installation for the demo and to allow API's to be built from scratch, the only two files of importance are:
  1. application/config/rest.php
  2. application/libraries/REST_Controller.php