PHP语言返回数据格式化类
小标 2018-07-12 来源 : 阅读 1563 评论 0

摘要:本文主要向大家介绍了PHP语言返回数据格式化类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

 本文主要向大家介绍了PHP语言返回数据格式化类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

php返回数据格式化类

DataReturn.class.php

[php] view plain copy

1. <?php  

2. /** 返回數據格式化類 

3. *   Date:   2011-08-15 

4. *   Author: fdipzone 

5. */  

6.   

7. class DataReturn{   // class start  

8.   

9.     private $type;  

10.     private $xmlroot;  

11.     private $callback;  

12.     private $returnData;  

13.   

14.     public function __construct($param=array()){  

15.         $this->type = $this->exists($param,'type')? strtoupper($param['type']) : 'JSON';      // 類型 JSON,XML,CALLBACK,ARRAY  

16.         $this->xmlroot = $this->exists($param,'xmlroot')? $param['xmlroot'] : 'xmlroot';      // xml root dom name  

17.         $this->callback = $this->exists($param,'callback')? $param['callback'] : '';          // JS callback function name  

18.   

19.         $format = array();  

20.         $format['retcode'] = $this->exists($param,'format.retcode')? $param['format']['retcode'] : 'retcode';//retcode 對應名稱  

21.         $format['msg'] = $this->exists($param,'format.msg')? $param['format']['msg'] : 'msg';                //msg 對應名稱  

22.         $format['data'] = $this->exists($param,'format.data')? $param['format']['data'] : 'data';            //data 對應名稱  

23.   

24.         $result = array();  

25.         $result[$format['retcode']] = $this->exists($param,'retcode')? $param['retcode'] : 0;  

26.         $result[$format['msg']] = $this->exists($param,'msg')? $param['msg'] : '';  

27.         $result[$format['data']] = $this->exists($param,'data')? $param['data'] : '';  

28.   

29.         $this->returnData = $result;  

30.     }  

31.   

32.   

33.     //輸出數據  

34.     public function data_return(){  

35.         ob_clean();  

36.         switch($this->type){  

37.             case 'JSON':  

38.                 $this->json_return();  

39.                 break;  

40.             case 'XML':  

41.                 $this->xml_return();  

42.                 break;  

43.             case 'CALLBACK':  

44.                 $this->callback_return();  

45.                 break;  

46.             case 'ARRAY':  

47.                 $this->array_return();  

48.                 break;  

49.             default:  

50.                 $this->json_return();  

51.         }  

52.         exit();  

53.     }  

54.   

55.   

56.     //輸出JSON格式數據,如有callback參數則返回JSONP格式  

57.     private function json_return(){  

58.         header('content-type:text/html;charset=utf-8');  

59.         if(empty($this->callback)){  

60.             echo json_encode($this->returnData);  

61.         }else{  

62.             echo $this->callback.'('.json_encode($this->returnData).');';  

63.         }  

64.     }  

65.   

66.   

67.     //輸出XML格式數據  

68.     private function xml_return(){  

69.         header('content-type:text/xml;charset=utf-8');  

70.         echo $this->xml_encode($this->returnData,$this->xmlroot);  

71.     }  

72.   

73.   

74.     //輸出JSON格式數據,并調用callback方法  

75.     private function callback_return(){  

76.         header('content-type:text/html;charset=utf-8');  

77.         $this->callback = empty($this->callback)? 'callback' : $this->callback;  

78.         echo "<script type=\"text/javascript\">\r\n";  

79.         echo $this->callback."(".json_encode($this->returnData).");\r\n";  

80.         echo "</script>";  

81.     }  

82.   

83.   

84.     //輸出數組格式數據  

85.     private function array_return(){  

86.         header('content-type:text/html;charset=utf-8');  

87.         echo '<pre>';  

88.         print_r($this->returnData);  

89.         echo '</pre>';  

90.     }  

91.   

92.   

93.     //XML編碼  

94.     private function xml_encode($data, $root='xmlroot', $encoding='utf-8') {  

95.         $xml = "<?xml version=\"1.0\" encoding=\"" . $encoding . "\"?>\n";  

96.         $xml.= "<" . $root . ">\n";  

97.         $xml.= $this->data_to_xml($data);  

98.         $xml.= "</" . $root . ">";  

99.         return $xml;  

100.     }  

101.   

102.   

103.     //數組轉XML格式  

104.     private function data_to_xml($data) {  

105.         if (is_object($data)) {  

106.             $data = get_object_vars($data);  

107.         }  

108.         $xml = '';  

109.         foreach ($data as $key => $val) {  

110.             is_numeric($key) && $key = "item id=\"$key\"";  

111.             $xml.="<$key>";  

112.             $xml.= ( is_array($val) || is_object($val)) ? $this->data_to_xml($val) : $this->cdata($val);  

113.             list($key, ) = explode(' ', $key);  

114.             $xml.="</$key>\n";  

115.         }  

116.         return $xml;  

117.     }  

118.   

119.   

120.     //判斷數據是否存在  

121.     private function exists($obj,$key=''){  

122.         if($key==''){  

123.             return isset($obj) && !empty($obj);  

124.         }else{  

125.             $keys = explode('.',$key);  

126.             for($i=0,$max=count($keys); $i<$max; $i++){  

127.                 if(isset($obj[$keys[$i]])){  

128.                     $obj = $obj[$keys[$i]];  

129.                 }else{  

130.                     return false;  

131.                 }  

132.             }  

133.             return isset($obj) && !empty($obj);  

134.         }  

135.     }  

136.   

137.   

138.     //判斷是否需要加上<![CDATA[]]>標記  

139.     private function cdata($val){  

140.         if(!empty($val) && !preg_match('/^[A-Za-z0-9+$]/',$val)){  

141.             $val = '<![CDATA['.$val.']]>';  

142.         }  

143.         return $val;  

144.     }  

145.   

146. }   // class end  

147. ?>  

demo

[php] view plain copy

1. <?  

2.     require_once('DataReturn.class.php');  

3.   

4.     $param = array( // DataReturn 參數  

5.                     'type'    => 'JSON', // 輸出的類型 JSON,XML,CALLBACK,ARRAY 默認為 JSON  

6.                     'retcode' => '1000', // retcode 的值,默認為0  

7.                     'msg'     => '',     // msg 的值,默認為空  

8.                     'data'    => array(  // 要輸出的數據  

9.                                     'id'     => '100',  

10.                                     'name'   => 'fdipzone',  

11.                                     'gender' => 1,  

12.                                     'age'    => 28  

13.                                  ),  

14.                     'format'  => array(  // 輸出的數據key格式,默認為 retcode,msg,data  

15.                                     'retcode' => 'status',  

16.                                     'msg'     => 'info',  

17.                                     'data'    => 'result'  

18.                                  ),  

19.                     'xmlroot'  => 'xmlroot',  // 當type=XML時,XML根節點名稱,默認為xmlroot  

20.                     'callback' => 'callback'  /* 回調方法名稱 

21.                                                    type=JSON時,默認為空,如不為空,則輸出callback({data}); 

22.                                                    type=CALLBACK時,默認為callback,自動調用頁面JS回調方法 

23.                                               */  

24.     );  

25.   

26.     $obj = new DataReturn($param);  // 創建DataReturn類對象  

27.     $obj->data_return();            // 按格式輸出數據  

28. ?>  

以上就介绍了PHP的相关知识,希望对PHP有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言PHP频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved