设为首页 收藏本站
查看: 848|回复: 0

[经验分享] PHP开发APP接口(一)

[复制链接]

尚未签到

发表于 2015-11-18 07:34:43 | 显示全部楼层 |阅读模式
  php以json或者xml 形式返回给app。明白这点就很好说了,就是把数据包装成json或者xml,返回给APP
  定义抽象APP基类:
  <?php
/**
* 定义API抽象类
*/
abstract class Api {
const JSON = 'Json';
const XML = 'Xml';
const ARR = 'Array';
/**
* 定义工厂方法
* param string $type 返回数据类型
*/
public static function factory($type = self::JSON) {
$type = isset($_GET['format']) ? $_GET['format'] : $type;
$resultClass = ucwords($type);
require_once('./Response/' . $type . '.php');
return new $resultClass();
}
abstract function response($code, $message, $data);
}

以xml形式返回给APP:
  <?php
class Xml extends Api {
public function response($code, $message = '', $data = array()) {
if(!is_numeric($code)) {
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);
header('Content-Type:text/xml');
$xml = &quot;<?xml version='1.0' encoding='UTF-8'?>\n&quot;;
$xml .= &quot;<root>&quot;;
$xml .= self::xmlToEncode($result);
$xml .= &quot;</root>&quot;;
echo $xml;
}
public static  function xmlToEncode($result) {
$xml = $attr = '';
foreach($result as $key => $value) {
//判断键值对,如果是数字键值不允许
if(is_numeric($key)) {
$attr = &quot; id='&quot; . $key . &quot;'&quot;;
$key = &quot;item&quot;;
}
$xml .= &quot;<{$key}{$attr}>&quot;;
//以递归形式返回,主要是因为数组在xml中显示是array,必须显示出来具体键值对
$xml .= is_array($value) ? self::xmlToEncode($value) : $value;
$xml .= &quot;</{$key}>\n&quot;;
}
return $xml;
}
}

以json&#26684;式返回数据:
  <?php
/**
* 按xml方式输出通信数据
*/
class Json extends Api {
public function response($code, $message = '', $data = array()) {
if(!(is_numeric($code))) {
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);
echo json_encode($result);
exit;
}
}

也可以采用这种方式组装返回数据:
  <?php
class Response {
const JSON = &quot;json&quot;;
/**
* 按综合方式输出通信数据
* @param integer $code 状态码
* @param string $message 提示信息
* @param array $data 数据
* @param string $type 数据类型
* return string
*/
public static function show($code, $message = '', $data = array(), $type = self::JSON) {
if(!is_numeric($code)) {
return '';
}
$type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
$result = array(
'code' => $code,
'message' => $message,
'data' => $data,
);
if($type == 'json') {
self::json($code, $message, $data);
exit;
} elseif($type == 'array') { //适合调试代码
var_dump($result);
} elseif($type == 'xml') {
self::xmlEncode($code, $message, $data);
exit;
} else {
// TODO
}
}
/**
* 按json方式输出通信数据
* @param integer $code 状态码
* @param string $message 提示信息
* @param array $data 数据
* return string
*/
public static function json($code, $message = '', $data = array()) {
if(!is_numeric($code)) {
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);
echo json_encode($result);
exit;
}
/**
* 按xml方式输出通信数据
* @param integer $code 状态码
* @param string $message 提示信息
* @param array $data 数据
* return string
*/
public static function xmlEncode($code, $message, $data = array()) {
if(!is_numeric($code)) {
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data,
);
header(&quot;Content-Type:text/xml&quot;);
$xml = &quot;<?xml version='1.0' encoding='UTF-8'?>\n&quot;;
$xml .= &quot;<root>\n&quot;;
$xml .= self::xmlToEncode($result);
$xml .= &quot;</root>&quot;;
echo $xml;
}
public static function xmlToEncode($data) {
$xml = $attr = &quot;&quot;;
foreach($data as $key => $value) {
if(is_numeric($key)) {
$attr = &quot; id='{$key}'&quot;;
$key = &quot;item&quot;;
}
$xml .= &quot;<{$key}{$attr}>&quot;;
$xml .= is_array($value) ? self::xmlToEncode($value) : $value;
$xml .= &quot;</{$key}>\n&quot;;
}
return $xml;
}
}





版权声明:本文为博主原创文章,转载请注明出处 http://blog.iyunv.com/buyingfei8888

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-140458-1-1.html 上篇帖子: 13款PHP开发框架,可供不同的移动后台开发者使用! 下篇帖子: 53个小技巧提高PHP编程效率
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表