




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、php基于curl扩展制作跨平台的restfule接口这篇文章主要介绍了 php基于curl扩展制作跨平台的restfule接口的相关资料以及详细的代 码,有需要的小伙伴可以参考下。restfule 接口适用的平台:跨平台所依赖:curl扩展git: apiserver.php<?php/* * author: yangyulong * email : anziguoersina* date: 2015-04-30 05:38:34 * last modified by: yangyulong * last modified time: 2015-04-30 17:14:11 */ cl
2、ass apiserver /* * 客户端请求的方式 * var string */ private $method =/*客户端发送的数据 * var type */ protected $param; /*要操作的资源* var type */ protected $resourse;/* * 要操作的资源 id * var type */protected $resourseld; /*构造函数,获取client请求的方式,以及传输的数据* param object 可以自定义传入的对彖 */ public function _construct() 首先对客户端的请求进行验证$thi
3、s->authorization();$this->method =strtolower($_server'request_method,);所有的请求都是 pathinfo 模式$pathinfo = s.serveri'pathjnfo1;将path info数据信息映射为实际请求方法$this->getresourse($pathinfo);获取传输的具体参数 $this->getdata();执行响应 $this->doresponse(); /*根据不同的请求方式,获取数据 *return type */ private function
4、 doresponse() switch ($this->method) case'get':$this->_get();break;case 'post':$this->_post();break;case 'delete1:$this->_delete();break;case 'put':$this->_put();break;default:$this->_get();break; /将 pathinfo数据信息映射为实际请求方法 private function getresourse($pa
5、thinfo) /*将pathinfo数据信息映射为实际请求方法*get /users:逐页列出所有用户; * post/users:创建一个新用户; * get/users/123: 返回用户为123的详细信息; * put/users/123:更新用户123;* delete /users/123:删除用户123;*根据以上规则,将pathinfo第一个参数映射为需要操作的数据表, * 第二个参数映射为操作的 id*/$info = explode(7'z ltrim($pathinfo,'/'); list($this->resourse, $this-&g
6、t;resourseld) = $info; /* 验证请求 */private function authorization()$toke n = $_server'http_client_token'$authorization = md5(substr(md5($toke n), 8, 24).$toke n);讦($authorization !=$_server*http_client_code')验证失败,输岀错误信息给客户端$this->output($status = 1); /* * getdata 获取传送的参数信息* paramtype $p
7、ad description* return typedescription*/ private functiongetdata()所有的参数都是 get 传参 $this->param = $_get; /* * 获取资源操作 * return type description */ protected function _get()逻辑代码根据白己实际项目需要实现/* *新增资源操作 * return type description*/ protected function _post()逻辑代码根据自己实际项目需要实现/* *删除资源操作 * return type descri
8、ption */ protected function _delete() 逻 辑代码根据自己实际项目需要实现/*更新资源操作 * return typedescription */ protected function _put()逻辑代码根据自己实际项目需耍实现/* 111 入服务端返回的数据信息 json 格式 */ public function output($stat,$data=array() $status = array(/a)状态表示请求成功0 =>array)'code' => 1,'info'=> '请求成功
9、39;data' =>$data),验证失败 1 => array('code' => 0,'info'=> '请求不合法');tryif(!in_array($stat, array_keys($status)throw new exception('输入的状态码不合法');elseecho json_encode($status$stat); catch(exception $e)die($e->getmessage(); apiclie nt.php<?php /* * creat
10、ed by phpstorm. * user: anziguoersina* date: 201 爭/29 * time: 12:36 * link: b log/2014yt)y restf u l_a p i. h t m i restf u i 设计指南*/* * * * * * * *定义路由的请求方式* * * $url_model=0* *采用传统的url参数模式* servername/appname/?m=module&a 二 actio n&id=l* * * * * * * * path info 模式 url_model 为 1 servername/ap
11、pname/module/actio n/id/1/* * */class restclient 请求的 token(默认模式)* *设置* * * *const token=,yangyulong' 请求 url private 请求的数据 private $data; $url;请求的类型 private $requesttype;/curl 实例 private $curl; public $status; private $headers = array); /* _construct构造方法,初始化数据* param type $url 请求的服务器地址 param type
12、 $requesttype 发送请求的方法* param type $data 发送的数据param integer $url_model 路由请求方式 array(), $requesttype = 'get1) 径 if (!$url) $paramurl = $value) $paramurl;=$data; 误); echo '</pre>f;curl_setopt($this->curl, curlopt_returntransfer, 1);请求的参数* return type description* lpost设置post请求的参数* post
13、新增资源public functio n _post() curl_setopt($this->curl, curlopt_posl 1);curl_setopt($this->curl, curlopt_postfields, $this->data);/* _put 设置 put 请求 * put 更新资源 * return type description */ public function _put() curl_setopt($this->curl, curlopt_customrequest, ,put,);/* tryreturn false;/ pat
14、h info 模式$paramllrl.= $key .,=' svalue.1 &初始化类屮的数据if (!$this->curl = curl_init()catch (exception $e)echo ,<pre>,;* param type $url* param type $data*/ public function _construct($url, $data = /url是必须要传的,并且是符合pathinfo模式的路 $this->requesttype = strtolower($requesttype); 讦(!empty($da
15、ta) foreach ($data as $key => $url = $url$this->url = $url;$this->datathrow new exception('curl 初始化错 print_r($e->getmessage(); curl_setopt($this->curl, curlopt_url, $this->url);/* _post 设置 get*/ public function _get() /* (©return type description*/_delete 删除资源 * delete 删除资
16、源 * return type description */ public function _delete() curl_setopt($this->curlz curlopt_customrequest 'delete'); /* * dorequest 执行发送请求 * return type description */ public function dorequest() 发送给服务端验证信息讦(null != self:token) &&self:token)$this->headers = array('clie nt_tok
17、e n: selfiitoke n,$this->getlnfo(); return $info; /* 设置发送的头部信息 */ privatefunction setheader() curl_setopt($this->curl, curlopt_httpheader, $this->headers); /* 生成 授权码 * return string 授权码 */ private functionsetauthorization()$authorization 二 md5(substr(md5(self:token), 8,24).self:token);retur
18、n $authorization; /* 获取 curl 屮 的状态信息 */ public functiongetlnfo()returncurl_getinfo($this->curl); /* * 关|用 curl 连接 */ public function _destruct()curl_close($this->curl); testclie nt.php<?php/* * created by phpstorm. * user: anziguoersina* date: 2015/1/29 * time: 12:35 */ in elude "./api
19、clie nt.php: $arr = array( 'user1 => qn ziguoer1, "passwd1 => *ya ngyulon g');/ $url =localhost/restapi/restserver.php'url = 'localhost/restapi/testserver.php/user/1231; $rest = new restclient($url, $arr, 'get');$info = $rest->dorequest(); 获取 curl 中的状态信息$status
20、= $rest->status;echo ,<pre>'print_r($info);echo ,</pre>'testserver.php<?php/* author:an ziguoersi na*email:an ziguoersi na*link: * date: 2015-04-30 16:52:53 * last modified by: yangyulong * last modified time: 2015-04-30 17:26:37 */ include './apiserver.php1; class testserver extends apiserver /* * 先执行 apise
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 信息系统监理师考试时间安排试题及答案
- 行政理论考试中的案例分析技巧试题及答案
- 持续学习信息系统监理师试题及答案
- 行政组织理论考试的策略与试题答案
- 2025年西方人权与法律审视试题及答案
- 公路工程实务操作试题及答案2024
- 电视剧剧本出版企业制定与实施新质生产力项目商业计划书
- 柔道混合赛行业深度调研及发展项目商业计划书
- 民族舞蹈表演在线平台行业跨境出海项目商业计划书
- 热气球场地在线平台企业制定与实施新质生产力项目商业计划书
- 中国血脂管理指南(基层版2024年)
- QBT 2959-2008 钢板网行业标准
- 大学生安全教育-知到答案、智慧树答案
- 走进歌剧世界智慧树知到期末考试答案章节答案2024年北京航空航天大学
- 口腔实习生培训
- DL-T 5148-2021水工建筑物水泥灌浆施工技术条件-PDF解密
- JJG 377-2019放射性活度计
- 《钢筋桁架楼承板应用技术规程》
- 家庭教育指导流程
- 整理收纳师课件
- DB11-T 2205-2023 建筑垃圾再生回填材料应用技术规程
评论
0/150
提交评论