# restAPI **Repository Path**: anziguoer/restAPI ## Basic Information - **Project Name**: restAPI - **Description**: php实现的restfu接口类 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 15 - **Forks**: 14 - **Created**: 2015-04-28 - **Last Updated**: 2021-09-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # **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`根据自己的业务逻辑实现代码** 4. 将自己的类实例化:`$testServer = new testServer()`; 5. 类ApiServer属性说明: `private $method = '';` 客户端发送请求的方法【get、post,put,delete】 `protected $param;` 客户端发送的参数 `protected $resourse;` 客户端发送的pathinfo第一个参数,请求的具体资源 `protected $resourseId` 客户端发送的pathinfo第二个参数,请求的具体资源的id,或者其他,此参数可不传 ---------- ####**服务端认证**: 1. 在apiclient.php 文件中 配置 //请求的token const token='xxx'; 即可。 #感觉不够完善,如有问题请指点,