require(APPPATH'.libraries/REST_Controller.php');
class Example_api extends REST_Controller {
}
?>
require(APPPATH'.libraries/REST_Controller.php');
class Example_api extends REST_Controller {
function user_get()
{
// 获得一个用户的信息
}
function users_get()
{
//获得多个用户的信息
}
}
?>
require(APPPATH'.libraries/REST_Controller.php');
class Example_api extends REST_Controller {
function user_get()
{
// 获得一个用户的信息
}
function user_put()
{
// 创建一个新用户
}
function user_post()
{
//更新用户信息
}
function user_delete()
{
//删除用户信息
}
}
?>
require(APPPATH'.libraries/REST_Controller.php');
class Example_api extends REST_Controller {
function user_get()
{
$data = array('returned: '. $this->get('id'));
$this->response($data);
}
function user_post()
{
$data = array('returned: '. $this->post('id'));
$this->response($data);
}
function user_put()
{
$data = array('returned: '. $this->put('id'));
$this->response($data;
}
function user_delete()
{
$data = array('returned: '. $this->delete('id'));
$this->response($data);
}
}
?>
require(APPPATH.'/libraries/REST_Controller.php');
class Api extends REST_Controller
{
function user_get()
{
if(!$this->get('id'))
{
$this->response(NULL, 400);
}
$user = $this->user_model->get( $this->get('id') );
if($user)
{
$this->response($user, 200); // 200 being the HTTP response code
}
else
{
$this->response(NULL, 404);
}
}
function user_post()
{
$result = $this->user_model->update( $this->post('id'), array(
'name' => $this->post('name'),
'email' => $this->post('email')
));
if($result === FALSE)
{
$this->response(array('status' => 'failed'));
}
else
{
$this->response(array('status' => 'success'));
}
}
function users_get()
{
$users = $this->user_model->get_all();
if($users)
{
$this->response($users, 200);
}
else
{
$this->response(NULL, 404);
}
}
}
?>
echo $user;
file_get_contents('http://admin:1234@example.com/index.php/api/user/id/1/format/json')
);
echo $user->name;
{
$username = 'admin';
$password = '1234';
// Alternative JSON version
// $url = 'http://twitter.com/statuses/update.json';
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'http://localhost/restserver/index.php/example_api/user/id/1/format/json');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(
'name' => $new_name,
'email' => $new_email
));
//本行可选,如果你的RESTful API是开放的,则请删除该行
curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
$result = json_decode($buffer);
if(isset($result->status) && $result->status == 'success')
{
echo 'User has been updated.';
}
else
{
echo 'Something has gone wrong';
}
}
{
$username = 'admin';
$password = '1234';
$this->load->library('curl');
$this->curl->create('http://localhost/restserver/index.php/example_api/user/id/1/format/json');
//本行可选,如果你的RESTful API是开放的,则请删除该行 $this->curl->http_login($username, $password);
$this->curl->post(array(
'name' => $new_name,
'email' => $new_email
));
$result = json_decode($this->curl->execute());
if(isset($result->status) && $result->status == 'success')
{
echo 'User has been updated.';
}
else
{
echo 'Something has gone wrong';
}
}
{
$this->load->library('rest', array(
'server' => 'http://localhost/restserver/index.php/example_api/',
'http_user' => 'admin',
'http_pass' => '1234',
'http_auth' => 'basic' // 或者使用'digest'
));
$user = $this->rest->get('user', array('id' => $id), 'json');
echo $user->name;
}
$user = $this->rest->get('users/show', array('screen_name' => 'philsturgeon'));
'server' => 'http://twitter.com/',
'http_user' => 'username',
'http_pass' => 'password',
'http_auth' => 'basic'
));
$user = $this->rest->post('statuses/update.json', array('status' => 'Using the REST client to do stuff'));