search
HomeBackend DevelopmentPHP TutorialAPP 接口开发及读取静态缓存,app读取静态缓存_PHP教程

APP 接口开发及读取静态缓存,app读取静态缓存

<span> 1</span> <?<span>php
</span><span> 2</span> <span>/*</span><span>*
</span><span> 3</span> <span> * Description: App 接口
</span><span> 4</span> <span> * Create date:2015-10-19 13:36
</span><span> 5</span> <span> * Author: zhaoyingnan
</span><span> 6</span> <span> *</span><span>*/</span>
<span> 7</span> <span>class</span><span> Response
</span><span> 8</span> <span>{
</span><span> 9</span>     <span>/*</span><span>*
</span><span>10</span> <span>     * 综合方式
</span><span>11</span> <span>     * @author  zhaoyingnan 2015-10-19 11:24
</span><span>12</span> <span>     * @param   int         $iCode  状态码
</span><span>13</span> <span>     * @param   string      $sMsg   提示信息                                                                                                                                                 
</span><span>14</span> <span>     * @param   mix         $mixData    data
</span><span>15</span> <span>     * @param   string      $sType  接口返回类型
</span><span>16</span> <span>     * @return  string
</span><span>17</span> <span>     *</span><span>*/</span>
<span>18</span>     <span>static</span> <span>function</span> show(<span>$iCode</span>, <span>$sMsg</span> = '', <span>$mixData</span> = '', <span>$sType</span> = 'json'<span>)
</span><span>19</span> <span>    {   
</span><span>20</span>         <span>if</span>(!<span>is_numeric</span>(<span>$iCode</span><span>))
</span><span>21</span>             <span>return</span> ''<span>; 
</span><span>22</span>         <span>$arData</span> =   <span>array</span><span>(
</span><span>23</span>             'code'      =>  <span>$iCode</span>,
<span>24</span>             'message'   =>  <span>$sMsg</span>,
<span>25</span>             'data'      =>  <span>$mixData</span>
<span>26</span> <span>        );  
</span><span>27</span>         <span>switch</span>(<span>$sType</span><span>)
</span><span>28</span> <span>        {   
</span><span>29</span>         <span>case</span> 'array':
<span>30</span>             <span>echo</span> '<pre class="brush:php;toolbar:false">'<span>;
</span><span>31</span>             <span>print_r</span>(<span>$arData</span><span>);
</span><span>32</span>             <span>echo</span> '
'; 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 ?>
<span> 1</span> <?<span>php                                                                                                                                                                                        
</span><span> 2</span> <span>/*</span><span>*
</span><span> 3</span> <span> * Description: 静态缓存
</span><span> 4</span> <span> * Create date:2015-10-19 13:36
</span><span> 5</span> <span> * Author: zhaoyingnan
</span><span> 6</span> <span> *</span><span>*/</span>
<span> 7</span> <span>class</span> <span>file</span>
<span> 8</span> <span>{
</span><span> 9</span>     <span>private</span> <span>$sExt</span>   =   '.txt'<span>;
</span><span>10</span> 
<span>11</span>     <span>/*</span><span>* 
</span><span>12</span> <span>     * 生成/删除/获取 缓存
</span><span>13</span> <span>     * @author  zhaoyingnan 2015-10-19 11:33
</span><span>14</span> <span>     * @param   string      $sKey       文件名
</span><span>15</span> <span>     * @param   mix         $mixValue   被缓存的数据(为''时表示获取缓存,为NUll时为删除缓存文件,否则为生成缓存)
</span><span>16</span> <span>     * @param   string      $sPath      文件保存的路径
</span><span>17</span> <span>     * @param   int         $iCacheTime 缓存时间(秒),0为永不过期    
</span><span>18</span> <span>     * @return  boolean
</span><span>19</span> <span>     *</span><span>*/</span>
<span>20</span>     <span>public</span> <span>function</span> cacheData(<span>$sKey</span>, <span>$mixValue</span> = '', <span>$sPath</span> = '/alidata/www/lianxi/file/', <span>$iCacheTime</span> = 0<span>)
</span><span>21</span> <span>    {   
</span><span>22</span>         <span>$sPath</span>  =   <span>rtrim</span>(<span>$sPath</span>, '/').'/'<span>;
</span><span>23</span>         <span>$sFileName</span>  =   <span>$sPath</span>.<span>$sKey</span>.<span>$this</span>-><span>sExt;
</span><span>24</span>         <span>//</span><span>生成缓存文件</span>
<span>25</span>         <span>if</span>(<span>$mixValue</span><span>)
</span><span>26</span> <span>        {   
</span><span>27</span>             <span>if</span>(!<span>is_dir</span>(<span>$sPath</span><span>))
</span><span>28</span>                 <span>mkdir</span>(<span>$sPath</span>, 0777<span>);
</span><span>29</span>             <span>$iCacheTime</span> =   <span>sprintf</span>('%011d', <span>$iCacheTime</span><span>);
</span><span>30</span>             <span>return</span> <span>file_put_contents</span>(<span>$sFileName</span>, <span>$iCacheTime</span>.json_encode(<span>$mixValue</span><span>));
</span><span>31</span> <span>        }   
</span><span>32</span> 
<span>33</span>         <span>if</span>(<span>is_file</span>(<span>$sFileName</span>) && !<span>$mixValue</span><span>)
</span><span>34</span> <span>        {   
</span><span>35</span>             <span>if</span>(<span>is_null</span>(<span>$mixValue</span><span>))
</span><span>36</span> <span>            {   
</span><span>37</span>                 <span>//</span><span>删除缓存</span>
<span>38</span>                 <span>return</span> <span>unlink</span>(<span>$sFileName</span><span>);
</span><span>39</span> <span>            }   
</span><span>40</span>                 
<span>41</span>             <span>//</span><span>获取缓存</span>
<span>42</span>             <span>$sContent</span>   =   <span>file_get_contents</span>(<span>$sFileName</span><span>);
</span><span>43</span>             <span>$iTime</span> = <span>intval</span>(<span>substr</span>(<span>$sContent</span>, 0, 11<span>));
</span><span>44</span>             <span>$sContent</span>   =   <span>substr</span>(<span>$sContent</span>, 11<span>);
</span><span>45</span>             <span>if</span>(<span>$iTime</span> != 0 && <span>$iTime</span> + <span>filemtime</span>(<span>$sFileName</span>) < <span>time</span><span>())
</span><span>46</span> <span>            {   
</span><span>47</span>                 <span>//</span><span>过期了,删除</span>
<span>48</span>                 <span>unlink</span>(<span>$sFileName</span><span>);
</span><span>49</span>                 <span>return</span> <span>FALSE</span><span>;
</span><span>50</span> <span>            }   
</span><span>51</span>             <span>return</span> <span>$sContent</span><span>;
</span><span>52</span> <span>        }
</span><span>53</span>         <span>else</span>
<span>54</span> <span>        {
</span><span>55</span>             <span>return</span> <span>FALSE</span><span>;
</span><span>56</span> <span>        }
</span><span>57</span> <span>    }
</span><span>58</span> <span>}
</span><span>59</span> ?>
<span> 1</span> <?<span>php                                                                                                                                                                                        
</span><span> 2</span> <span>include</span> 'response.php'<span>;
</span><span> 3</span> <span>include</span> 'file.php'<span>;
</span><span> 4</span> <span>$_GET</span>['format'] =   <span>isset</span>(<span>$_GET</span>['format']) && <span>in_array</span>(<span>$_GET</span>['format'], <span>array</span>('xml', 'json', 'array')) ? <span>$_GET</span>['format'] : 'json'<span>;
</span><span> 5</span> <span>$file</span> = <span>new</span> <span>File</span><span>();
</span><span> 6</span> <span>//</span><span>删除缓存
</span><span> 7</span> <span>//exit(var_dump($file->cacheData('index_cache', null)));</span>
<span> 8</span> 
<span> 9</span> <span>if</span>(!<span>$sContent</span> = <span>$file</span>->cacheData('index_cache'<span>))
</span><span>10</span> <span>{
</span><span>11</span>     <span>//</span><span>echo "获取缓存失败\n";
</span><span>12</span> <span>    //echo "获取数据\n";</span>
<span>13</span>     <span>$arData</span> =   <span>array</span><span>(
</span><span>14</span>         'id'    =>  1,  
<span>15</span>         'name'  =>  'TeddyNan',
<span>16</span>         'sex'   =>  23, 
<span>17</span>         <span>array</span><span>(
</span><span>18</span>             'nani'=><span>array</span><span>(
</span><span>19</span>                 'g'=>'gg',
<span>20</span>                 2,  
<span>21</span>                 4   
<span>22</span> <span>            )   
</span><span>23</span> <span>        )   
</span><span>24</span> <span>    );  
</span><span>25</span>     <span>//</span><span>echo "生成缓存\n";</span>
<span>26</span> 
<span>27</span>     <span>$file</span>->cacheData('index_cache', <span>$arData</span>, '/alidata/www/lianxi/file/', 0<span>); 
</span><span>28</span>     Response::show(0, 'success', <span>$arData</span>, <span>$_GET</span>['format'<span>]);
</span><span>29</span> <span>}
</span><span>30</span> <span>else</span>
<span>31</span> <span>{
</span><span>32</span>     Response::show(0, 'success', json_decode(<span>$sContent</span>, <span>TRUE</span>), <span>$_GET</span>['format'<span>]);
</span><span>33</span> <span>}
</span><span>34</span> ?>

 

www.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...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software