How to set it up I read the tutorial http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814. But I am unable to get the idea, I want more details. I am very new to CodeIgniter and to API.
I did the following steps from nettuts article
REST SERVER :
this is the server that listen to client (restClient) request.
RESTServer has the Request methods :
POST()
GET()
PUT()
DELETE()
and these are use like index_put();
keep in mind when you called it from RESTClient ,you will called it like :
$this->index();
not
$this->index_put(); //because restserver it self recognize the nature of request through header.
Here is a simple example:
RESTClient:
function request_test() {
$this->load->library('rest', array(
'server' => 'http://restserver.com/customapi/api/',
//when not use keys delete these two liness below
'api_key' => 'b35f83d49cf0585c6a104476b9dc3694eee1ec4e',
'api_name' => 'X-API-KEY',
));
$created_key = $this->rest->post('clientRequest', array(
'id' => '1',
'CustomerId' => '1',
'amount' => '2450',
'operatorName' => 'Jondoe',
), 'json');
print_r($created_key);
die;
}
RESTSERVER:
<?php
require APPPATH . '/libraries/REST_Controller.php';
class api extends REST_Controller {
public function clientRequest_post() {
//to get header
$headers=array();
foreach (getallheaders() as $name => $value) {
$headers[$name] = $value;
}
//to get post data
$entityBody = file_get_contents('php://input', 'r');
parse_str($entityBody , $post_data);
//giving response back to client
$this->response('success', 200);
}
}
configuration
config/Rest.php
:
//if you need no authentication see it's different option in the same file
$config['rest_auth'] = false;
//for enabling/disabling API_KEYS
$config['rest_enable_keys'] = FALSE;