Home > Article > Backend Development > php-app interface implementation (json and xml)
1. Review
In the previous article, we learned about the instantiated object class that encapsulates mysql!
2. This article will encapsulate an app interface class to generate json data and xml data
3. Understand and master
3.1 The difference between xml and json
xml: extended tag Language: You can mark data and define data types; the data format is clear and readable;
json: a lightweight data exchange format; simple to generate data, fast transmission speed;
3.2 app interface and Data
.
Status code: code
Prompt information: message off 3.4 Scheduled tasks and cache (not encapsulated here)
Cache: static cache, Memcache And Redis cache technology i timing task: Corntab
4. Packaging
4.1 JSON packaging
function json_encode ();
4.2 xml encapsulationAssembling string (simple)
4.3 Implementation class
<?php //header("Content-type:text/html;charset=utf-8"); class Response{ const JSON='json'; /** * 01.综合通信入口 * @param int $code * @param string $msg * @param array $data * @param string $type */ public static function show($code,$msg='',$data=array()){ if(!is_numeric($code)){ return ''; } //如果url上传参了,去参数的类型,否则取得默认值! $type=isset($_GET['type'])?$_GET['type']:self::JSON; $result=array( 'code'=>$code, 'msg'=>$msg, 'data'=>$data ); if($type=='json'){ self::jsonEncode($code,$msg,$data); exit(); }elseif ($type=='xml'){ self::xmlEncode($code,$msg,$data); exit(); }elseif ($type='array'){ var_dump($result); exit(); } } /** * 02.按json方式输出 通信数据 * @param int $code 状态码 * @param string $msg 提示信息 * @param array $data 数据 * retrun string */ public static function jsonEncode($code,$msg='',$data=array()) { header("Content-Type:text/json"); #判断状态码 if(!is_numeric($code)){ return ''; } $result=array( 'code'=>$code, 'msg'=>$msg, 'data'=>$data ); echo json_encode($result); exit(); } /** * 03.封装xml 输出通信数据 * @param unknown $code * @param unknown $msg * @param unknown $data */ public static function xmlEncode($code,$msg='',$data=array()){ if(!is_numeric($code)){ return ''; } $result=array( 'code'=>$code, 'msg'=>$msg, 'data'=>$data ); header("Content-Type:text/xml"); $xml="<?xml version='1.0' encoding='UTF-8'?>"; $xml.="<root>"; $xml.=self::xmlToEncode($result); $xml.="</root>"; echo $xml; exit(); } /** *04. 拼装 xml数据 * @param array $data * @return string * 使用递归,判断是不是数组,是数组继续调用循环 * xml的 节点不能为 数字,用item代替 */ public static function xmlToEncode($data){ $xml=$attr=''; foreach ($data as $key=>$value){ if(is_numeric($key)){ $attr="id='{$key}'"; $key="item"; } $xml.="<{$key} {$attr}>"; $xml.=is_array($value)?self::xmlToEncode($value):$value; $xml.="</{$key}>"; } return $xml; } } ?>
4.4 Call
Implemented through url, display The data type can be xml, json, array!
http://localhost:8081/appInterface/test.php?type=jsonTest.php is implemented as follows:
require_once 'appUtil.php';
$arr=array( 'id'=>1, 'name'=>'yuan', 'age'=>23, 'location'=>'hpu' ); $arr1=array(1,4,5,2,6,3); Response::jsonEncode(200,'success',$arr);
5. In summary, the previous article is connected to mysql database implementation: data encapsulation
//调用 $con=Db::getInstance()->connect(); //查询语句 $sql='select * from user_info'; //执行,返回结果集 $result=mysql_query($sql,$con); //添加的新数组 $arr3=array(); while ($row=mysql_fetch_row($result)){ array_push($arr3,$row); } Response::show('200','success',$arr3);
6. appUtil.php download
http://download.csdn.net/detail/lablenet/8995987
Copyright statement: This article is an original article by the blogger, without the blogger's permission No reproduction allowed.
The above introduces the php-app interface implementation (json and xml), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.