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

WBOY
发布: 2016-07-13 10:08:48
原创
860 人浏览过

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>);
登录后复制

 

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...
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板