restserver 和 restclient 如何在 CodeIgniter 中协同工作

How restserver and restclient working together in CodeIgniter

如何设置 我阅读了教程 http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814。但我无法理解,我想要更多细节。我是 CodeIgniter 和 API.

的新手

我根据 nettuts 文章

执行了以下步骤

REST SERVER :

这是监听客户端(restClient)请求的服务器。 RESTServer 具有 Request 方法: POST()
获取()
放()
删除()

这些用法类似于 index_put(); 请记住,当您从 RESTClient 调用它时,您会这样调用它:

$this->index();

不是

$this->index_put(); //because restserver it self recognize the nature of request through header.

这是一个简单的例子:

REST客户端:

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;

    }
  • 确保加载了 rest 库。

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;