php ci3.0 restful,在CodeIgniter框架中使用RESTful服务

其实只需要把rest.php和libraries目录下的REST_Controller.php复制到你的应用中的相应位置即可。这里假设要从你的应用的后端实体模型中进行相关操作,并将操作发布成RESTful的webservice,因此修改代码如下:

php

require(APPPATH."/libraries/REST_Controller.php");

class Api extends REST_Controller

{

function user_get()

{

{

$this->response(NULL, 400);

}

$user = $this->user_model->get( $this->get("id") );

{

$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")

));

{

$this->response(array("status" => "failed"));

}

else

{

$this->response(array("status" => "success"));

}

}

function users_get()

{

$users = $this->user_model->get_all();

{

$this->response($users, 200);

}

else

{

$this->response(NULL, 404);

}

}

}

以上的代码段其实之前都已经介绍过,只不过这次在每个方法中,读取的是后端数据库中的实体类而已。

步骤5 保护RESTful API

为了保护RESTful API,可以在application/config/rest.php中设置安全保护级别,如下所示:

$config["rest_auth"] = "basic";

其中保护级别有如下设置:

None:任何人都可以访问这个API

BASIC:这个设置中,只提供基本的验证方式,适合在内部网络使用

Digest:使用用户名和密码进行验证,比如:$config["rest_valid_logins"] = array("admin" => "1234");

第二部分 调用RESTful服务

接下来,我们讲解如何调用RESTful服务,因为RESTful服务是通过HTTP协议进行发布服务,因此有多种方法进行调用。

1) 通过file_get_contents()调用

可以通过PHP中最简单的file_get_contents()去调用RESTful,比如:

$user = json_decode(file_get_contents("http://example.com/index.php/api/user/id/1/format/json"));

echo $user;

要注意的是,要是访问一个受密码保护的RESTful的话,需要用如下形式访问:

$user = json_decode(

file_get_contents("http://admin:1234@example.com/index.php/api/user/id/1/format/json")

);

echo $user->name;

2)使用cUrl访问RESTful

使用php中的cUrl去访问RESTful服务时最方便稳定的,下面的代码指导如何使用cUrl去设置HTTP协议头,去更新一个指定用户的信息,如下代码所示:

function native_curl($new_name, $new_email)

{

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

{

echo "User has been updated.";

}

else

{

echo "Something has gone wrong";

}

}

用过PHP的cUrl的朋友应该知道,这里其实是使用cUrl对我们已经发布的RESTful地址进行访问,并提交相关要更新的参数。但是,强大的 CodeIgniter为我们封装了更强大的cUrl类库cUrl library(http://codeigniter.com/wiki/Curl_library/),上面的代码可以简化如下:

function ci_curl($new_name, $new_email)

{

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

{

echo "User has been updated.";

}

else

{

echo "Something has gone wrong";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值