首页 > 网站 > 建站经验 > 正文

php基于curl扩展制作跨平台!的restfule 接口

2019-11-02 15:38:27
字体:
来源:转载
供稿:网友

   这篇文章主要介绍了php基于curl扩展制作跨平台的restfule 接口的相关资料以及详细的代码,有需要的小伙伴可以参考下。

  restfule 接口

  适用的平台:跨平台

  所依赖:curl扩展

  git:https://git.oschina.net/anziguoer/restAPI

  ApiServer.php

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 <?php /** * @Author: yangyulong * @Email : [email protected] * @Date: 2015-04-30 05:38:34 * @Last Modified by: yangyulong * @Last Modified time: 2015-04-30 17:14:11 */   class apiServer { /** * 客户端请求的方式 * @var string */ private $method = '';   /** * 客户端发送的数据 * @var [type] */ protected $param;   /** * 要操作的资源 * @var [type] */ protected $resourse;   /** * 要操作的资源id * @var [type] */ protected $resourseId;     /** * 构造函数, 获取client 请求的方式,以及传输的数据 * @param object 可以自定义传入的对象 */ public function __construct() { //首先对客户端的请求进行验证 $this->authorization();   $this->method = strtolower($_SERVER['REQUEST_METHOD']);   //所有的请求都是pathinfo模式 $pathinfo = $_SERVER['PATH_INFO'];   //将pathinfo数据信息映射为实际请求方法 $this->getResourse($pathinfo);   //获取传输的具体参数 $this->getData();   //执行响应 $this->doResponse(); }   /** * 根据不同的请求方式,获取数据 * @return [type] */ private function doResponse(){ switch ($this->method) { case 'get': $this->_get(); break; case 'post': $this->_post(); break; case 'delete': $this->_delete(); break; case 'put': $this->_put(); break; default: $this->_get(); break; } }   // 将pathinfo数据信息映射为实际请求方法 private function getResourse($pathinfo){   /** * 将pathinfo数据信息映射为实际请求方法 * GET /users: 逐页列出所有用户; * POST /users: 创建一个新用户; * GET /users/123: 返回用户为123的详细信息; * PUT /users/123: 更新用户123; * DELETE /users/123: 删除用户123; * * 根据以上规则,将pathinfo第一个参数映射为需要操作的数据表, * 第二个参数映射为操作的id */   $info = explode('/', ltrim($pathinfo, '/')); list($this->resourse, $this->resourseId) = $info; }   /** * 验证请求 */ private function authorization(){ $token = $_SERVER['HTTP_CLIENT_TOKEN']; $authorization = md5(substr(md5($token), 8, 24).$token); if($authorization != $_SERVER['HTTP_CLIENT_CODE']){ //验证失败,输出错误信息给客户端 $this->outPut($status = 1); } }   /** * [getData 获取传送的参数信息] * @param [type] $pad [description] * @return [type] [description] */ private function getData(){ //所有的参数都是get传参 $this->param = $_GET; }   /** * 获取资源操作 * @return [type] [description] */ protected function _get(){ //逻辑代码根据自己实际项目需要实现 }   /** * 新增资源操作 * @return [type] [description] */ protected function _post(){ //逻辑代码根据自己实际项目需要实现 }   /** * 删除资源操作 * @return [type] [description] */ protected function _delete(){ //逻辑代码根据自己实际项目需要实现 }   /** * 更新资源操作 * @return [type] [description] */ protected function _put(){ //逻辑代码根据自己实际项目需要实现 }   /** * 出入服务端返回的数据信息 json格式 */ public function outPut($stat, $data=array()){ $status = array( //0 状态表示请求成功 0 => array( 'code' => 1, 'info' => '请求成功', 'data' =>$data ), //验证失败 1 => array( 'code' => 0, 'info' => '请求不合法' ) );   try{ if(!in_array($stat, array_keys($status))){ throw new Exception('输入的状态码不合法'); }else{ echo json_encode($status[$stat]); } }catch (Exception $e){ die($e->getMessage()); } } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表