首页 > Windows开发 > 详细

CI中如何保护RESTful API

时间:2015-03-23 11:03:11      阅读:261      评论:0      收藏:0      [点我收藏+]

步骤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);
技术分享
技术分享        if(isset($result->status) && $result->status == ‘success‘)
技术分享        {
技术分享            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());
技术分享        if(isset($result->status) && $result->status == ‘success‘)
技术分享        {
技术分享            echo ‘User has been updated.‘;
技术分享        }
技术分享        else
技术分享        {
技术分享            echo ‘Something has gone wrong‘;
技术分享        }
技术分享    }
 
http://tech.chinaunix.net/a2011/0420/1180/000001180783_3.shtml

CI中如何保护RESTful API

原文:http://www.cnblogs.com/kenshinobiy/p/4359107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!