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
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Have you tried with default tutorial code? Does it work? – Tpojka Jun 03 '15 at 17:15
  • thanks for reply.yes I first I try the example – MichaelMHerbert Jun 03 '15 at 17:34
  • It doesn't work with fresh CI installation and default tutorial files/code? Do you claim that? – Tpojka Jun 03 '15 at 17:41
  • I just only run example from rest-server and it give me the error use ci version 3 and I am using ci version 2.2 – MichaelMHerbert Jun 03 '15 at 17:48
  • I am little confuse here Part 2 - Interacting with RESTful Services – MichaelMHerbert Jun 03 '15 at 18:18
  • Yes, you are right. Code for REST server is meant to be used with CI3 and PHP 5.4. But in [description](https://github.com/chriskacerguis/codeigniter-restserver#requirements) is mentioned [downloading for old(er) versions](https://github.com/chriskacerguis/codeigniter-restserver/releases). Try this or upgrade CI to 3.0. – Tpojka Jun 03 '15 at 18:22
  • thanks @Tpojka it work perfect now. – MichaelMHerbert Jun 03 '15 at 18:58
  • I am glad I could help. Happy coding. – Tpojka Jun 03 '15 at 19:12
  • I am still confuse where to check my restful-server api and how to get user input. – MichaelMHerbert Jun 04 '15 at 11:15
  • 1
    What do you try? You need to edit your question and write down the code you are using to help us understand where is the problem. I like to mention [this source](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) as great one helped me and many. – Tpojka Jun 04 '15 at 16:11

1 Answers1

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;
Abdul Manan
  • 2,255
  • 3
  • 27
  • 51