APP 接口开发及读取静态缓存,app读取静态缓存
程序员文章站
2022-05-06 19:57:41
...
APP 接口开发及读取静态缓存,app读取静态缓存
1 php 2 /** 3 * Description: App 接口 4 * Create date:2015-10-19 13:36 5 * Author: zhaoyingnan 6 **/ 7 class Response 8 { 9 /** 10 * 综合方式 11 * @author zhaoyingnan 2015-10-19 11:24 12 * @param int $iCode 状态码 13 * @param string $sMsg 提示信息 14 * @param mix $mixData data 15 * @param string $sType 接口返回类型 16 * @return string 17 **/ 18 static function show($iCode, $sMsg = '', $mixData = '', $sType = 'json') 19 { 20 if(!is_numeric($iCode)) 21 return ''; 22 $arData = array( 23 'code' => $iCode, 24 'message' => $sMsg, 25 'data' => $mixData 26 ); 27 switch($sType) 28 { 29 case 'array': 30 echo ''; 31 print_r($arData); 32 echo ''; 33 break; 34 case 'xml': 35 self::xml($arData); 36 break; 37 default: 38 self::json($arData); 39 } 40 } 41 42 /** 43 * json 44 * @author zhaoyingnan 2015-10-19 10:21 45 * @param array $arData 46 * @return string 47 **/ 48 private function json($arData= array()) 49 { 50 exit(json_encode($arData)); 51 } 52 53 /** 54 * xml 55 * @author zhaoyingnan 2015-10-19 10:21 56 * @param array $arData 57 * @return string 58 **/ 59 private function xml($arData = array()) 60 { 61 header('Content-Type:text/xml'); 62 $sXml = ''; 63 $sXml .= "\n"; 64 $sXml .= "\n"; 65 $sXml .= self::xmlEncode($arData); 66 $sXml .= " \n"; 67 exit($sXml); 68 } 69 70 /** 71 * xml encode 72 * @author zhaoyingnan 2015-10-19 11:10 73 * @param array $arData 74 * @return string 75 **/ 76 private function xmlEncode($arData = array()) 77 { 78 if(!$arData) 79 return ''; 80 $sXml = $sAttr= ''; 81 foreach($arData as $mKey => $mVal) 82 { 83 if(is_numeric($mKey)) 84 { 85 $sAttr = " id='{$mKey}'"; 86 $mKey = 'item'; 87 } 88 $sXml .= is_array($mVal) ? self::xmlEncode($mVal) : "$mKey