PHP实现RESTful风格的API实例(二)
Response.php :包含一个Request类,即输出类。根据接收到的Content-Type,将Request类返回的数组拼接成对应的格式,加上header后输出
<?php
/**
* 输出类
*/
class Response
{
const HTTP_VERSION = "HTTP/1.1"; //返回结果
public static function sendResponse($data)
{
//获取数据
if ($data) {
$code = 200;
$message = 'OK';
} else {
$code = 404;
$data = array('error' => 'Not Found');
$message = 'Not Found';
} //输出结果
header(self::HTTP_VERSION . " " . $code . " " . $message);
$content_type = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : $_SERVER['HTTP_ACCEPT'];
if (strpos($content_type, 'application/json') !== false) {
header("Content-Type: application/json");
echo self::encodeJson($data);
} else if (strpos($content_type, 'application/xml') !== false) {
header("Content-Type: application/xml");
echo self::encodeXml($data);
} else {
header("Content-Type: text/html");
echo self::encodeHtml($data);
}
} //json格式
private static function encodeJson($responseData)
{
return json_encode($responseData);
} //xml格式
private static function encodeXml($responseData)
{
$xml = new SimpleXMLElement('<?xml version="1.0"?><rest></rest>');
foreach ($responseData as $key => $value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$xml->addChild($k, $v);
}
} else {
$xml->addChild($key, $value);
}
}
return $xml->asXML();
} //html格式
private static function encodeHtml($responseData)
{
$html = "<table border='1'>";
foreach ($responseData as $key => $value) {
$html .= "<tr>";
if (is_array($value)) {
foreach ($value as $k => $v) {
$html .= "<td>" . $k . "</td><td>" . $v . "</td>";
}
} else {
$html .= "<td>" . $key . "</td><td>" . $value . "</td>";
}
$html .= "</tr>";
}
$html .= "</table>";
return $html;
}
}
index.php :入口文件,调用Request类取得数据后交给Response处理,最后返回结果
<?php
//数据操作类
require('Request.php');
//输出类
require('Response.php');
//获取数据
$data = Request::getRequest();
//输出结果
Response::sendResponse($data);
PHP实现RESTful风格的API实例(二)的更多相关文章
- PHP实现RESTful风格的API实例(三)
接前一篇PHP实现RESTful风格的API实例(二) .htaccess :重写URL,使URL以 /restful/class/1 形式访问文件 Options +FollowSymlinks R ...
- PHP实现RESTful风格的API实例(一)
最近看了一些关于RESTful的资料,自己动手也写了一个RESTful实例,以下是源码 目录详情: restful/ Request.php 数据操作类 Response.php 输出类 index. ...
- Gin实战:Gin+Mysql简单的Restful风格的API(二)
上一篇介绍了Gin+Mysql简单的Restful风格的API,但代码放在一个文件中,还不属于restful风格,接下来将进行进一步的封装. 目录结构 ☁ gin_restful2 tree . ├─ ...
- PHP实现RESTful风格的API实例
原生方式实现,直接撸代码 Request.php :包含一个Request类,即数据操作类.接收到URL的数据后,根据请求URL的方式(GET|POST|PUT|PATCH|DELETE)对数据进行相 ...
- restful风格的API
在说restful风格的API之前,我们要先了解什么是rest.什么是restful.最后才是restful风格的API! PS(REST:是一组架构约束条件和原则,REST是Roy Thomes F ...
- 测开大佬告诉你:如何5分钟快速创建restful风格的API接口-使用django restframework框架
一.思考❓❔ 1.创建API接口难吗? 软件测试工程师: 只测过API接口, 从没创建过 应该需要掌握一门后端开发语言和后端开发框架吧!? 脑容量有限,想想就可怕 2.如何创建API接口呢? 使用Dj ...
- PHP实现Restful风格的API
Restful是一种设计风格而不是标准,比如一个接口原本是这样的: http://www1.qixoo.com/user/view/id/1表示获取id为1的用户信息,如果使用Restful风格,可以 ...
- [01] 浅谈RESTful风格的API
1.什么是RESTful风格的API REST,即Representational State Transfer,可以理解为"(资源的)表现层状态转化". 在网络上,我们通过浏览器 ...
- Gin实战:Gin+Mysql简单的Restful风格的API
我们已经了解了Golang的Gin框架.对于Webservice服务,restful风格几乎一统天下.Gin也天然的支持restful.下面就使用gin写一个简单的服务,麻雀虽小,五脏俱全.我们先以一 ...
随机推荐
- Tomcat服务器8080端口占用报错
报错信息如下: Oct , :: PM org.apache.coyote.AbstractProtocol init SEVERE: Failed to initialize end point a ...
- Async and Await
http://blog.stephencleary.com/2012/02/async-and-await.html Most people have already heard about the ...
- CentOS添加用户及赋予sudo权限
在CentOS下添加一个用户假设用户名:username 密码:userpwd 新建用户: adduser username 修改密码: passwd username 根据提示重复输入两遍userp ...
- [UCSD白板题 ]Small Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- 数据库、数据表的创建SP2
本人前一个版本的数据库.数据表的创建由于不是很详细,于是通过细心的修订,已经修复了很多Bug,希望这篇文章能够给大家一些帮助 --代表注释,相当于C#里的// --切换到master数据库,目的是 ...
- jquery对象操作
大类 JQ方法 备注 创建元素 var $h1 = $(“<h1>< ...
- 网页播放器(jsp、js)
jsp对控件显示 <%@ page language="java" import="java.util.*" pageEncoding="UTF ...
- redis持久化以及主从服务器的配置
作者:silenceper 日期:2013-10-03 原文地址:http://silenceper.com/archives/959.html redis 与memcached 最大的一个区别就是R ...
- IOS 计时器暂停和开始 防止重复点击
-(IBAction)btnClick{ [self starTimer];//开始计时 //[self stopTimer]; } -(NSTimer*)timer{ if (!_timer) { ...
- Focus, w/o disturbance
Focus = Aim + Execution + Persistence Disturbance = Disappointment + Anxiety + Failure