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 '<pre>'; 31 print_r($arData); 32 echo '</pre>'; 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 .= "<?xml version='1.0' encoding='UTF-8'?>\n"; 64 $sXml .= "<root>\n"; 65 $sXml .= self::xmlEncode($arData); 66 $sXml .= "</root>\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 ?>