时间:2021-07-01 10:21:17 帮助过:5人阅读
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}{$sAttr}>{$mVal}{$mKey}>"; 89 } 90 return $sXml; 91 } 92 } 93 ?>
1 php 2 /** 3 * Description: 静态缓存 4 * Create date:2015-10-19 13:36 5 * Author: zhaoyingnan 6 **/ 7 class file 8 { 9 private $sExt = '.txt'; 10 11 /** 12 * 生成/删除/获取 缓存 13 * @author zhaoyingnan 2015-10-19 11:33 14 * @param string $sKey 文件名 15 * @param mix $mixValue 被缓存的数据(为''时表示获取缓存,为NUll时为删除缓存文件,否则为生成缓存) 16 * @param string $sPath 文件保存的路径 17 * @param int $iCacheTime 缓存时间(秒),0为永不过期 18 * @return boolean 19 **/ 20 public function cacheData($sKey, $mixValue = '', $sPath = '/alidata/www/lianxi/file/', $iCacheTime = 0) 21 { 22 $sPath = rtrim($sPath, '/').'/'; 23 $sFileName = $sPath.$sKey.$this->sExt; 24 //生成缓存文件 25 if($mixValue) 26 { 27 if(!is_dir($sPath)) 28 mkdir($sPath, 0777); 29 $iCacheTime = sprintf('%011d', $iCacheTime); 30 return file_put_contents($sFileName, $iCacheTime.json_encode($mixValue)); 31 } 32 33 if(is_file($sFileName) && !$mixValue) 34 { 35 if(is_null($mixValue)) 36 { 37 //删除缓存 38 return unlink($sFileName); 39 } 40 41 //获取缓存 42 $sContent = file_get_contents($sFileName); 43 $iTime = intval(substr($sContent, 0, 11)); 44 $sContent = substr($sContent, 11); 45 if($iTime != 0 && $iTime + filemtime($sFileName) < time()) 46 { 47 //过期了,删除 48 unlink($sFileName); 49 return FALSE; 50 } 51 return $sContent; 52 } 53 else 54 { 55 return FALSE; 56 } 57 } 58 } 59 ?>
1 php 2 include 'response.php'; 3 include 'file.php'; 4 $_GET['format'] = isset($_GET['format']) && in_array($_GET['format'], array('xml', 'json', 'array')) ? $_GET['format'] : 'json'; 5 $file = new File(); 6 //删除缓存 7 //exit(var_dump($file->cacheData('index_cache', null))); 8 9 if(!$sContent = $file->cacheData('index_cache')) 10 { 11 //echo "获取缓存失败\n"; 12 //echo "获取数据\n"; 13 $arData = array( 14 'id' => 1, 15 'name' => 'TeddyNan', 16 'sex' => 23, 17 array( 18 'nani'=>array( 19 'g'=>'gg', 20 2, 21 4 22 ) 23 ) 24 ); 25 //echo "生成缓存\n"; 26 27 $file->cacheData('index_cache', $arData, '/alidata/www/lianxi/file/', 0); 28 Response::show(0, 'success', $arData, $_GET['format']); 29 } 30 else 31 { 32 Response::show(0, 'success', json_decode($sContent, TRUE), $_GET['format']); 33 } 34 ?>
http://www.bkjia.com/PHPjc/1061852.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1061852.htmlTechArticleAPP 接口开发及读取静态缓存,app读取静态缓存 1 ? php 2 /* * 3 * Description: App 接口 4 * Create date:2015-10-19 13:36 5 * Author: zhaoyingnan 6 * */ 7 class Res...