APP 介面開發及讀取靜態緩衝,app讀取靜態緩衝_PHP教程

來源:互聯網
上載者:User

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:2412      * @param   int         $iCode  狀態代碼13      * @param   string      $sMsg   提示資訊                                                                                                                                                 14      * @param   mix         $mixData    data15      * @param   string      $sType  介面傳回型別16      * @return  string17      **/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'      =>  $mixData26         );  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 * json44 * @author zhaoyingnan 2015-10-19 10:2145 * @param array $arData46 * @return string 47 **/48 private function json($arData= array())49 { 50 exit(json_encode($arData));51 } 52 53 /**54 * xml55 * @author zhaoyingnan 2015-10-19 10:2156 * @param array $arData57 * @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 encode72 * @author zhaoyingnan 2015-10-19 11:1073 * @param array $arData74 * @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:3314      * @param   string      $sKey       檔案名稱15      * @param   mix         $mixValue   被緩衝的資料(為''時表示擷取緩衝,為NUll時為刪除快取檔案,否則為產生緩衝)16      * @param   string      $sPath      檔案儲存的路徑17      * @param   int         $iCacheTime 緩衝時間(秒),0為永不到期    18      * @return  boolean19      **/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         else54         {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 else31 {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...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.