Thursday 12 February 2015

Create Restful webservices using CodeIgniter

There is a very good tutorial on that website:
http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814

You need to have  CodeIgniter already set-up (if not, you can go to http://www.codeigniter.com/ and follow to instruction for installation: http://www.codeigniter.com/userguide3/installation/index.html) 

Next, you will have to download the restserver Library  (cf. https://github.com/chriskacerguis/codeigniter-restserver) and merge the application folder with that of CodeIgniter website;

Two folders should be updated : application\library (3 files) et application\config (1 file : rest.php)

Next, we will show how to generate a simple RestFUL XML service:

Create a file for the controler within application\controllers: lets name it : hellows .php :



Not, instead of writing a basic CodeIgniter controler like 


<?php
class hellows extends CI_Controller {

}
...you will need to use:

<?php

require(APPPATH.'libraries/REST_Controller.php');

class hellows extends REST_Controller {


}

Working with Resources

Next, you will need to know what will be the ressource to call : say you want to call the world ressource ! you will then need to define 'world_get' function within that class if you want to access this ressource using a GET request (that will then be world_post for a POST request)


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require(APPPATH.'libraries/REST_Controller.php');


class hellows extends REST_Controller {


    function world_get() {
      // respond with information about world ressource
    }

}

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.
Here is the content of the file when accessing parameters and returning data:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require(APPPATH.'libraries/REST_Controller.php');

class hellows extends REST_Controller {
        
    function world_get() {
        $data["returned"]= $this->get('id');
        $data['name'] = 'HELLO';
        $data['university'] = 'coventry';
        $this->response($data, 200);    
    }   
    
}

This example contains 2 new pieces of code:
Is used to return GET variables from either a query string like this index.php/hellows/world?id=1 or can be set in the more CodeIgniter'esque way with index.php/hellows/world/id/1.
Sends data to the browser in whichever data format has been requested, or defaults to XML. You can optionally pass a HTTP status code to show it has worked or failed. E.g if the ID provided was not in the database, you could use $this->response(array('error' => 'parameter not found.'), 404);

When calling the following URL:  http://localhost/CodeIgniter/index.php/hellows/world?id=1

You will get a JSON or XML file depending of  $config['rest_default_format'] value (i.e. 'json' or 'xml') in config/rest.php file;

{
  • returned"1",
  • name"HELLO",
  • university"coventry"
}

Now, if you want to specify a different format (xml), you can also do it through the URL call:

http://localhost/CodeIgniter/index.php/hellows/world/id/1/format/xml

<xml><returned>1</returned><name>HELLO</name><university>coventry</university></xml>