本文主要向大家介绍了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 "
79. echo $this->callback."(".json_encode($this->returnData).");\r\n";
80. echo "";
81. }
82.
83.
84. //輸出數組格式數據
85. private function array_return(){
86. header('content-type:text/html;charset=utf-8');
87. echo '
';
88. print_r($this->returnData);
89. echo '
';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.= "\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.="";
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
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. //判斷是否需要加上標記
139. private function cdata($val){
140. if(!empty($val) && !preg_match('/^[A-Za-z0-9+$]/',$val)){
141. $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频道!