>php教程 >php手册 >PHP返回JSON和XML类,返回jsonxml

PHP返回JSON和XML类,返回jsonxml

WBOY
WBOY원래의
2016-06-13 09:16:171282검색

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>);

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.