最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
使用CodeIgniter 创建 RESTful 服务 REST API
时间:2013-07-13 编辑:简简单单 来源:一聚教程网
介绍
在现在这个时代,你很容易把例如blog,CMS等等的服务整合到自己的网站里面。有一件事你也许没想过就是利用CodeIgniter去创建API接口。尝试几个RESTful服务的实现方法后,我发现过程非常麻烦,而且缺少你所期望的一些RESTful的功能。所以我自己建了一个,这个教程将告诉你怎样去利用这些代码创建属于自己的REST API,而且我将举些例子告诉你如何与自己搭建的API进行交互。
假设
■你有自己的web 服务器,可以是本地localhost的,也可以是线上的,而且你知道如何去管理里面的问文件
■你必须阅读过CodeIgniter from Scratch教程(译者注:也就是懂得使用CodeIgniter框架)
■懂得怎么设置CodeIgniter
■懂一点点RESTful 服务知识
这个教程将分成2个部分讲。第一部分将从学习如何搭建RESTful 服务说起,然后深入,第二部分将讲如何用多种方式与自己搭建的REST API交互。
第一部分—创建REST API
第一步:建立demo
首先,你需要从Git中下载一份 codeigniter-restserver 代码回来。提取里面的代码,放置你的web server里面(译者注:下载方式如图所示,原作者的图片是Git改版前的,不适用于现在,所以本人斗胆替换掉了)
打开“application/config/config.php” ,根据自己的情况设置base_url。。(译者注:其实就是CI的根目录的地址,这个我想用过CI的都懂吧。如果你真的什么都不懂,那么你把下载回来后的文件解压,然后重命名为restserver,把整个文件夹丢到WWW根目录下,把base_url设置成如图所示)
- xml – 几乎所有的语言都能识别XML格式
- json – 对javascript和PHP APP非常有用. (译者注:其实还有一个jsonp格式)
- csv – 可以被电子表格程序打开
- html – 不解释
- php – 返回的PHP是可以被eval函数执行过的
- serialized – 序列化格式,对PHP非常有用(译者注:原文有误,记得是有个d结尾)
- class Example_api extends Controller {
- }
- 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()
- {
- // 返回关于1个用户的信息
- }
- function users_get()
- {
- // 返回所有用户的信息
- }
- }
不解释
比较少用的一个HTTP动作,大多数浏览器都不支持,PUT动作用于创建一个资源。(译者注:与delete对应)
和PUT一样,很少用到。DELETE动作用于删除一个资源.
(译者注:其实GET,POST已经可以完全替代PUT和DELETE的功能,所以后面2个动作非常少用到)
- require(APPPATH’.libraries/REST_Controller.php’);
- class Example_api extends REST_Controller {
- function user_get()
- {
- // respond with information about a user
- // 返回一个用户信息
- }
- function user_put()
- {
- // create a new user and respond with a status/errors
- // 创建一个新用户和返回对应的状态码
- }
- function user_post()
- {
- // update an existing user and respond with a status/errors
- // 更新一个已存在的用户信息和返回对应的状态码
- }
- function user_delete()
- {
- // delete a user and respond with a status/errors
- // 删除一个用户和返回响应状态码
- }
- }
- 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);
- }
- }
接收put参数
- application/config/rest.php
- application/libraries/REST_Controller.php
- 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)&
nbsp; - {
- $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);
- }
- }
- }
- ?>
- $config['rest_auth'] = ‘basic’; //设置REST权限
设置为None的话,所有人都可以调用这个API.
设置为Basic的话,是一个相对不安全的登录方法,它应该只用于内部/安全网络.
这是一个更安全的选项,需要加密的用户名和密码,如果你想受保护的API让有权限的人调用,使用这个选项。
- $config['rest_valid_logins'] = array(‘admin’ => ’1234′);
相关文章
- Codeigniter MongoDB扩展之使用Aggregate实现Sum方法 05-29
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31