gpt4 book ai didi

web-services - Codeigniter 网络服务

转载 作者:行者123 更新时间:2023-12-03 14:54:35 24 4
gpt4 key购买 nike

我正在使用 Codeigniter 1.7。有没有人有使用 PHP 创建 Web 服务的经验,尤其是在 CodeIgniter 框架中?实现 Web 服务时需要考虑哪些安全措施?如何使用 API key 提供身份验证?

有任何想法吗?

最佳答案

这取决于您查询的网络服务类型。例如,Web 服务会成为守护进程吗?或典型的在线网络服务。对于其中任何一个,您都必须实现 RESTful 类型。 RESTful 意味着无状态连接。这是使用 API key 的地方;例如,识别用户。

幸运的是 Codeigniter 是一个包含许多库和扩展的工具。此类库的示例可以在这里:https://github.com/philsturgeon/codeigniter-restserver

现在出于安全考虑:API key 将取代 session 或任何状态。您必须对 api 进行全面检查。许多实现 API 的站点为相同的最终结果提供了不同的解决方案。

使用 API key 进行身份验证很简单。您将根据存储类型(数据库)对其进行检查。

这是使用 codeigniter 和之前链接的库的教程:http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

这可能有点模糊,但是由于您没有任何具体问题或明显的需求,因此很难具体说明。

编辑:

在这种情况下,最好实现一个 RESTful 接口(interface),以便您的 iphone 应用程序也可以使用您的服务提供的所有用户功能。最好的方法是以一种方式使所有东西都可以访问。这意味着 iphone 连接和 Web 连接没有不同的 Controller /型号。

因此,例如,您可以拥有以下 Controller :

<?php

class Auth extends CI_Controller{

public function login(){
//Check if their accessing using a RESTful interface;
$restful = $this->rest->check();
if($restful){
//Check for the API keys;
$apiKey = $this->input->get('apiKey');
$secretKey = $this->input->get('secretKey');

//If you have any rules apon the keys you may check it (i.e. their lengths,
//character restrictions, etc...)
if(strlen($apiKey) == 10 and strlen($secretKey) == 14)
{
//Now check against the database if the keys are acceptable;
$this->db->where('apiKey', $apiKey);
$this->db->where('secretKey', $secretKey);
$this->db->limit(1);
$query = $this->db->get('keys');
if($this->db->count_all_results() == 1)
{
//It's accepted the keys now authenticate the user;
foreach ($query->result() as $row)
{
$user_id = $row->user_id;
//Now generate a response key;
$response_key = $this->somemodel->response_key($user_id);
//Now return the response key;
die(json_encode( array(
'response_key' => $response_key,
'user_id' => $user_id
)
)
);

} //End of Foreach
}//End of Result Count
}//End of length / character check;
} else {
//Perform your usual session login here...;

}
}
}

?>

现在这只是执行这些类型请求的一个小例子。这可以适用于任何类型的 Controller 。虽然这里有几个选项。您可以让每个请求都传递 apikey 和 key ,并在每个请求中验证它。或者,您可以拥有某种白名单,一旦您第一次通过验证,之后的每个请求都将被列入白名单,或者在相反的情况下被列入黑名单。

希望这可以帮助,
丹尼尔

关于web-services - Codeigniter 网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9696009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com