PHP implementation of the return data format class Instance _php tutorial

Source: Internet
Author: User

PHP implementation of the return data format class instance


The DataReturn.class.php class file is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

/** Return Data Formatting class

* DATE:2011-08-15

* Author:fdipzone

*/

Class datareturn{//Class start

Private $type;

Private $xmlroot;

Private $callback;

Private $returnData;

Public function __construct ($param =array ()) {

$this->type = $this->exists ($param, ' type ')? Strtoupper ($param [' type ']): ' JSON '; Type Json,xml,callback,array

$this->xmlroot = $this->exists ($param, ' XmlRoot ')? $param [' XmlRoot ']: ' XmlRoot '; XML root DOM Name

$this->callback = $this->exists ($param, ' callback ')? $param [' Callback ']: '; JS callback Function name

$format = Array ();

$format [' retcode '] = $this->exists ($param, ' Format.retcode ')? $param [' Format '] [' retcode ']: ' Retcode ';//retcode corresponding name

$format [' msg '] = $this->exists ($param, ' format.msg ')? $param [' Format '] [' msg ']: ' msg '; MSG corresponding Name

$format [' data '] = $this->exists ($param, ' Format.data ')? $param [' Format '] [' data ']: ' Data '; Data corresponding name

$result = Array ();

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

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

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

$this->returndata = $result;

}

Output data

Public Function Data_return () {

Ob_clean ();

Switch ($this->type) {

Case ' JSON ':

$this->json_return ();

Break

Case ' XML ':

$this->xml_return ();

Break

Case ' CALLBACK ':

$this->callback_return ();

Break

Case ' ARRAY ':

$this->array_return ();

Break

Default

$this->json_return ();

}

Exit ();

}

Output JSON format data, return JSONP format if callback parameter is available

Private Function Json_return () {

Header (' Content-type:text/html;charset=utf-8 ');

if (Empty ($this->callback)) {

Echo Json_encode ($this->returndata);

}else{

echo $this->callback. ' ('. Json_encode ($this->returndata). ';

}

}

Output XML Format data

Private Function Xml_return () {

Header (' Content-type:text/xml;charset=utf-8 ');

echo $this->xml_encode ($this->returndata, $this->xmlroot);

}

Output JSON format data and call the callback method

Private Function Callback_return () {

Header (' Content-type:text/html;charset=utf-8 ');

$this->callback = Empty ($this->callback)? ' Callback ': $this->callback;

echo "";

}

Output Array Format data

Private Function Array_return () {

Header (' Content-type:text/html;charset=utf-8 ');

Echo '

Echo '

';

}

XML encoding

Private Function Xml_encode ($data, $root = ' XmlRoot ', $encoding = ' utf-8 ') {

$xml = " \ n";

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

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

$xml. = " ";

return $xml;

}

Array to XML format

Private Function Data_to_xml ($data) {

if (Is_object ($data)) {

$data = Get_object_vars ($data);

}

$xml = ";

foreach ($data as $key = = $val) {

Is_numeric ($key) && $key = "Item id=\" $key \ "";

$xml. = "< $key >";

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

List ($key,) = Explode ("', $key);

$xml. = " \ n";

}

return $xml;

}

Determine if the data exists

Private function exists ($obj, $key = ") {

if ($key = = ") {

return Isset ($obj) &&!empty ($obj);

}else{

$keys = Explode ('. ', $key);

For ($i =0, $max =count ($keys), $i < $max; $i + +) {

if (Isset ($obj [$keys [$i]]) {

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

}else{

return false;

}

}

return Isset ($obj) &&!empty ($obj);

}

}

Determine if you need to add a tag

Private Function CDATA ($val) {

if (!empty ($val) &&!preg_match ('/^[a-za-z0-9+$]/', $val)) {

$val = " '.$val.' ;

}

return $val;

}

}//Class end

?>

The Demo sample program is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

!--? php

Require_once (' DataReturn.class.php ');

$param = Array (///DataReturn parameter

' type ' = ' json ',//output type Json,xml,callback,array default to JSON

' RET The value of code ' = ' + ',///Retcode, defaults to 0

' msg ' = ', '//msg ' value, default is null

' data ' = = Array (//outputs to be output

' id ' = ', '

' name ' = ' Fdipzone ',

' Gender ' = 1,

' Age ' =

),

' format ' = = Array (//Output Data key format, default = Retcode,msg,data

' retcode ' = ' status ',

' msg ') = ' Info ',

' data ' = ' result '

),

' xmlroot ' + ' xmlroot ',//when type=xml, XML root node name, default to XM Lroot

' callback ' = ' callback '/* callback method name

Type=json, default is NULL, if not NULL, output callback ({data});

Type=callback, the default is callback, automatically invoke the page JS callback method

*/

);

$obj = new DataReturn ($param);//Create DataReturn Class object

$obj->data_return ();//output data by format

;

p>

http://www.bkjia.com/PHPjc/882703.html www.bkjia.com true http://www.bkjia.com/PHPjc/882703.html techarticle PHP Implementation of the return data format class instance DataReturn.class.php class file is as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 It's a ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.