PHP返回JSON和XML类,返回jsonxml_PHP教程

WBOY
Libérer: 2016-07-13 10:08:48
original
849 Les gens l'ont consulté

PHP返回JSON和XML类,返回jsonxml

<span> 1</span> <?<span>php
</span><span> 2</span>     <span>class</span><span> Reponse{
</span><span> 3</span>         <span>//</span><span>private $result = array('code'=null,'message'=null,'data'=>null);</span>
<span> 4</span>         <span>/*</span><span>*
</span><span> 5</span> <span>         * @desc 返回JSON格式
</span><span> 6</span> <span>         * @param int $code 
</span><span> 7</span> <span>         * @param string $message 
</span><span> 8</span> <span>         * @param array  $data
</span><span> 9</span> <span>         * return string
</span><span>10</span>          <span>*/</span>
<span>11</span>         <span>public</span> <span>static</span> <span>function</span> json(<span>$code</span>,<span>$message</span> = <span>null</span>,<span>$data</span> = <span>array</span><span>()){
</span><span>12</span>             <span>if</span>(!<span>is_numeric</span>(<span>$code</span><span>)){
</span><span>13</span>                 <span>return</span> <span>false</span><span>;
</span><span>14</span> <span>            }
</span><span>15</span>             <span>$result</span> = <span>array</span><span>(
</span><span>16</span>                 'code'=><span>$code</span>,
<span>17</span>                 'message'=><span>$message</span>,
<span>18</span>                 'data'=><span>$data</span>
<span>19</span> <span>            );
</span><span>20</span>             <span>return</span> json_encode(<span>$result</span><span>);
</span><span>21</span>             <span>exit</span><span>;
</span><span>22</span> <span>        }
</span><span>23</span>         
<span>24</span>         <span>/*</span><span>*
</span><span>25</span> <span>         * @desc 返回xml格式数据
</span><span>26</span> <span>         * @parma int $code 状态码
</span><span>27</span> <span>         * @param string $message 提示
</span><span>28</span> <span>         * @param array $data 数据
</span><span>29</span> <span>         * return string 
</span><span>30</span>          <span>*/</span>
<span>31</span>          <span>public</span> <span>static</span> <span>function</span> xml(<span>$code</span>,<span>$message</span> = '',<span>$data</span> = <span>array</span><span>()){
</span><span>32</span>             <span>if</span>(!<span>is_numeric</span>(<span>$code</span><span>)){
</span><span>33</span>                 <span>return</span> <span>false</span><span>;
</span><span>34</span> <span>            }
</span><span>35</span>             <span>$result</span> = <span>array</span><span>(
</span><span>36</span>                 'code'=><span>$code</span>,
<span>37</span>                 'message'=><span>$message</span>,
<span>38</span>                 'data'=><span>$data</span>
<span>39</span> <span>            );
</span><span>40</span>             <span>$xml</span> = ''<span>;
</span><span>41</span>             
<span>42</span>             <span>$xml</span> .= "<?xml version='1.0' encoding='UTF-8'?>\n"<span>;
</span><span>43</span>             <span>$xml</span> .= "<root>\n"<span>;
</span><span>44</span>             <span>$xml</span> .= self::xmlEncode(<span>$result</span><span>);
</span><span>45</span>             <span>$xml</span> .= "</root>"<span>;
</span><span>46</span>             <span>header</span>("Content-Type:text/xml"<span>);
</span><span>47</span>             <span>echo</span> <span>$xml</span><span>;
</span><span>48</span> 
<span>49</span> <span>         }
</span><span>50</span>          
<span>51</span>          <span>public</span> <span>static</span> <span>function</span> xmlEncode(<span>$result</span><span>){
</span><span>52</span>             <span>$xml</span> = <span>$attr</span> =''<span>;
</span><span>53</span>             <span>foreach</span>(<span>$result</span> <span>as</span> <span>$key</span>=><span>$val</span><span>){
</span><span>54</span>                 <span>if</span>(<span>is_numeric</span>(<span>$key</span><span>)){
</span><span>55</span>                     <span>$attr</span> = "id='{<span>$key</span>}'"<span>;
</span><span>56</span>                     <span>$key</span> = "item{<span>$key</span>}"<span>;
</span><span>57</span> <span>                }
</span><span>58</span>                 <span>$xml</span> .= "<{<span>$key</span>} {<span>$attr</span>}>"<span>;
</span><span>59</span>                 
<span>60</span>                 <span>$xml</span> .= <span>is_array</span>(<span>$val</span>) ? self::xmlEncode(<span>$val</span>) : <span>$val</span><span>;
</span><span>61</span>             
<span>62</span>                 <span>$xml</span> .= "</{<span>$key</span>}>\n"<span>;
</span><span>63</span> <span>            }
</span><span>64</span>          
<span>65</span>             <span>return</span> <span>$xml</span><span>;
</span><span>66</span>          
<span>67</span> <span>         }
</span><span>68</span>          
<span>69</span> <span>    }
</span><span>70</span>     
<span>71</span>     <span>$data</span> = <span>array</span><span>(
</span><span>72</span>         'id'=>1,
<span>73</span>         'age'=>20,
<span>74</span>         'username'=>'tim',
<span>75</span>         'others'=><span>array</span>(1,2,3),
<span>76</span> <span>    );
</span><span>77</span>     Reponse::xml(200,'success',<span>$data</span><span>);
</span><span>78</span>     Reponse::json(200,'success',<span>$data</span>);
Copier après la connexion

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/948636.htmlTechArticlePHP返回JSON和XML类,返回jsonxml 1 ? php 2 class Reponse{ 3 // private $result = array('code'=null,'message'=null,'data'=null); 4 /* * 5 * @desc 返回JSON格式 6 * @param i...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!