Codeigniter Web服务

siv*_*565 16 authentication soap web-services codeigniter

我正在使用Codeigniter 1.7.有没有人有使用PHP创建Web服务的经验,特别是在CodeIgniter框架内?实施Web服务时需要考虑哪些安全措施?如何使用API​​密钥提供身份验证?

有任何想法吗?

Dan*_*iel 16

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

幸运的是Codeigniter是一个有很多库和扩展的.这种库的一个例子可以在这里:https://github.com/philsturgeon/codeigniter-restserver

现在出于安全考虑:API密钥将替换会话或任何状态.你必须对api进行全面检查.许多实现API的站点为相同的最终结果提供了不同的解决方案.

使用API​​密钥进行身份验证很简单 您可以根据存储类型(数据库)进行检查.

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

这可能有点模糊,但由于你没有任何具体的问题或明显的需求,很难具体.

编辑:

在这种情况下,最好实现RESTful接口,以便您的iphone应用程序也可以使用您的服务提供的所有用户功能.最好的方法是以一种方式使一切都可访问.意味着没有用于iphone连接和Web连接的不同控制器/型号.

例如,您可以拥有以下控制器:

<?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...;

      }
   }
}

?>
Run Code Online (Sandbox Code Playgroud)

现在,这只是执行这些类型请求的一个小例子.这可以适用于任何类型的控制器.虽然这里有一些选择.您可以使每个请求都通过apikey,并且每次都要通过秘密,并在每次请求时验证它.或者,您可以拥有某种白名单,一旦您在第一次验证后将其列入白名单,或者在相反的情况下列入黑名单.

希望这有帮助,丹尼尔