其實只需要把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";
}
}