如何设置它--我读了教程http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814。但是我无法理解这个想法,我想要更多的细节。我对CodeIgniter和API非常陌生。
我从nettuts文章中执行了以下步骤
发布于 2015-06-13 12:13:32
REST服务器:
这是侦听客户端(restClient)请求的服务器。RESTServer有请求方法: POST()
得到()
放()
删除()
当您从index_put();调用它时,请记住,您可以这样称呼它:
$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;https://stackoverflow.com/questions/30625499
复制相似问题