7 Star 15 Fork 14

anziguoer / restAPI

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Restful 风格接口

####更新说明

  1. 请求增加验证方法 2015.05.10

#####文件说明:

  1. ApiClient.php 客户端请求类
  2. ApiServer.php 服务端响应类
  3. testClient.php 客户端使用示例代码
  4. testServer.php 服务端响应示例代码

####客户端请求说明:

  1. 引入ApiClient.php文件

  2. 参数说明: $url 请求服务端地址pathinfo模式【http://serverName/appName/module/action/user/1】 $data 传递给服务器的数据参数 $requestType 请求类型,暂时支持get,post,put,delete;此参数可不传,则默认请求为get方式

  3. 实例化类  $client = new apiClient($url, $data, $requestType);

  4. 执行方法 $result = $client->doRequest()获取服务端返回的数据


服务端响应说明:

  1. 自定义服务端类,引入ApiServer.php文件
  2. 自定义类需要继承apiServer类
  3. include './ApiServer.php'; class testServer extends apiServer { /** * 先执行apiServer中的方法,初始化数据 * @param object $obj 可以传入的全局对象[数据库对象,框架全局对象等] */ private $obj; function __construct(object $obj) { parent::__construct(); $this->obj = $obj; //$this->resourse; 父类中已经实现,此类中可以直接使用 //$tihs->resourseId; 父类中已经实现,此类中可以直接使用 print_r($this->param); } /** * 获取资源操作 * @return [type] [description] */ protected function _get(){ echo "get";//测试用 //逻辑代码根据自己实际项目需要实现 } /** * 新增资源操作 * @return [type] [description] */ protected function _post(){ echo "post";//测试用 //逻辑代码根据自己实际项目需要实现 } /** * 删除资源操作 * @return [type] [description] */ protected function _delete(){ //逻辑代码根据自己实际项目需要实现 } /** * 更新资源操作 * @return [type] [description] */ protected function _put(){ echo "put";//测试用 //逻辑代码根据自己实际项目需要实现 } } $server = new testServer();

您可以复制此代码到您自定义的类文件中,类中的方法 _get(), _post, _delete, _put根据自己的业务逻辑实现代码

  1. 将自己的类实例化:$testServer = new testServer();

  2. 类ApiServer属性说明:

    private $method = ''; 客户端发送请求的方法【get、post,put,delete】 protected $param; 客户端发送的参数 protected $resourse; 客户端发送的pathinfo第一个参数,请求的具体资源 protected $resourseId 客户端发送的pathinfo第二个参数,请求的具体资源的id,或者其他,此参数可不传


####服务端认证

  1. 在apiclient.php 文件中 配置 //请求的token const token='xxx'; 即可。 #感觉不够完善,如有问题请指点,
The MIT License (MIT) Copyright (c) 2015 anziguoer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

php实现的restfu接口类 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/anziguoer/restAPI.git
git@gitee.com:anziguoer/restAPI.git
anziguoer
restAPI
restAPI
master

搜索帮助