0

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

  • download both restclient and restserver and curl
  • I try to run examples from rest-server it does not show anything to me. I load my own controller and methods
4

1 回答 1

3

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;

    }
  • Make sure you loaded rest library.

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;
于 2015-06-13T12:13:32.030 回答