Как restserver и restclient работают вместе в CodeIgniter

Как это настроить, я прочитал учебник http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2—net-8814. Но я не могу понять, я хочу больше деталей. Я очень плохо знаком с CodeIgniter и API.

Я сделал следующие шаги из статьи Nettuts

  • скачать как restclient, так и restserver и curl
  • Я пытаюсь запустить примеры с rest-сервера, он мне ничего не показывает. Я загружаю свой собственный контроллер и методы

0

Решение

REST SERVER:

это сервер, который прослушивает клиентский запрос (restClient).
RESTServer имеет методы запроса:
СООБЩЕНИЕ()
ПОЛУЧИТЬ()
ПОЛОЖИЛ()
УДАЛЯТЬ()

и это использовать как index_put(); имейте в виду, когда вы вызывали его из RESTClient, вы будете называть его так:

$this->index();

не

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

Вот простой пример:

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);


}
}

конфигурация 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;
3

Другие решения

Других решений пока нет …