博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 使用 restler 框架构建 restfull api
阅读量:7281 次
发布时间:2019-06-30

本文共 1452 字,大约阅读时间需要 4 分钟。

php 使用 restler 框架构建 restfull api 

restler 轻量级,小巧,构建restfull api非常方便!

官网:http://restler3.luracast.com/

目前最后的是3.0版本,我测试使用的是2.2版本;

一:安装restler2

1. 下载 https://github.com/Luracast/Restler/tree/2.2.0

2. exmple目录下放的是例子

3. 把restler 目录的所有文件放到你的web服务下;

二:开始创建restfull api

1. index.php

setSupportedFormats('JsonFormat'); //接口列表文件 $r->addAPIClass('api'); $r->handle(); ?>

2. 主接口文件 api.php, 上述1中的api表示的类名api

"error "); } if ($deviceType=='1') { //return ios return array('xml' =>"ios"); } elseif ($deviceType=='2') { //return android return array('xml' =>"android"); } elseif ($deviceType=='3') { //return pc return array('xml' =>"pc"); } else { return array('xml' =>"none support deviceType"); } } //post 请求, public function postXXL($dev) { return returnXML($dev); } //当类名与文件名相同时,可以不用 include 该类 public function getAAA() { $bd = new Baidu(); return $ret = array('site' => "baidu.com", );; } } ?>

 

三:访问测试

1. api.php中所有public的方法,就是请求的方法,get或post 以方法名的前关键字为准;

 如getXml方法,测试请求方式为

  get http://127.0.0.1/tp/api/xml.josn

  或 http://127.0.0.1/tp/api/xml/1.json

  或http://127.0.0.1/tp/api/xml?deviceType=1

  后面的1对应getXml的请求字段 $deviceType

2. 其他函数同上

3. 如果要同时支持xml

  如http://127.0.0.1/tp/api/xml/1.xml

    http://127.0.0.1/tp/api/xml/1.json

 在index.php配置

$r->setSupportedFormats('JsonFormat', 'XmlFormat');

 

更多:http://restler3.luracast.com/examples/index.html

 

转载于:https://www.cnblogs.com/cocoajin/p/5800168.html

你可能感兴趣的文章
有意思的网站
查看>>
max3232
查看>>
linux读写ntfs
查看>>
x264 编码器选项分析 (x264 Codec Strong and Weak Points) 1
查看>>
lintcode:带环链表
查看>>
10最好的开放源移动工具的工作场所
查看>>
【转载】为什么不建议<=3G的情况下使用CMS GC
查看>>
CentOS中基于不同版本安装重复包的解决方案
查看>>
vim:查看当前的配置文件名称和地址
查看>>
javaweb学习总结二十二(servlet开发中常见的问题汇总)
查看>>
Nginx 做系统的前端反向proxy
查看>>
Win8 Metro(C#)数字图像处理--2.49Zhang二值图像细化算法
查看>>
嵌入 Office ,doc|docx|xls|xlsx|ppt|pptx|pdf|等
查看>>
Cut Down QtWebkit Library
查看>>
在Windows下Hunchentoot的启动
查看>>
网新恒天2013年校园招聘笔试
查看>>
推荐五星级C语言学习网站
查看>>
windows 下的命令行工具。。
查看>>
使用自定义的BaseAdapter实现LIstView的展示
查看>>
对RTMP视频流进行BitmapData.draw()出错的解决办法
查看>>