PHP WeChat public Platform Development class Example _php tutorial

Source: Internet
Author: User
Tags addchild php class

PHP Public Platform Development Class example


The thinkwechat.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

138

139

140

141

142

Class Wechat {

/**

* Push-over data or response data

* @var Array

*/

Private $data = Array ();

/**

* Constructor method for instantiating the SDK

* @param string $token token for open platform settings

*/

Public function __construct ($token) {

$this->auth ($token) | | Exit

if (!empty ($_get[' echostr ')) {

Exit ($_get[' echostr ');

} else {

Try

{

$xml = file_get_contents ("Php://input");

$xml = new SimpleXMLElement ($xml);

$xml | | Exit

foreach ($xml as $key = = $value) {

$this->data[$key] = Strval ($value);

}

}catch (Exception $e) {

}

}

}

/**

* Get Push data

* @return Array to convert data to arrays

*/

Public Function request () {

return $this->data;

}

/**

* * Respond to messages sent (auto reply)

* @param string $to receive user name

* @param string $from Sender user Name

* @param array $content reply message, text message is string type

* @param string $type message type

* @param string $flag Whether the newly received information

* @return String XML string

*/

Public Function response ($content, $type = ' text ', $flag = 0) {

/* Basic Data */

$this->data = Array (

' Tousername ' = $this->data[' fromusername '],

' Fromusername ' = $this->data[' tousername '],

' Createtime ' = Time (),

' Msgtype ' = $type,

);

/* Add type Data */

$this-$type ($content);

/* Add status */

$this->data[' funcflag ') = $flag;

/* Convert data to XML */

$xml = new SimpleXMLElement (" );

$this->data2xml ($xml, $this->data);

Exit ($xml->asxml ());

}

/**

* Reply to text message

* @param string $content information to reply to

*/

Private function Text ($content) {

$this->data[' Content ' = $content;

}

/**

* Reply to music information

* @param string $content The music to reply to

*/

Private function Music ($music) {

List

$music [' Title '],

$music [' Description '],

$music [' Musicurl '],

$music [' Hqmusicurl ']

) = $music;

$this->data[' Music ') = $music;

}

/**

* Reply to graphic information

* @param string $news text content to reply to

*/

Private Function News ($news) {

$articles = Array ();

foreach ($news as $key = = $value) {

List

$articles [$key] [' Title '],

$articles [$key] [' Description '],

$articles [$key] [' Picurl '],

$articles [$key] [' URL ']

) = $value;

if ($key >= 9) {break;}//Up to only 10 news is allowed

}

$this->data[' articlecount '] = count ($articles);

$this->data[' articles ') = $articles;

}

/**

* Data XML encoding

* @param an object $xml XML object

* @param mixed $data data

* Node name when @param string $item a numeric index

* @return String

*/

Private Function Data2xml ($xml, $data, $item = ' item ') {

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

/* Specify the default number key */

Is_numeric ($key) && $key = $item;

/* Add child elements */

if (Is_array ($value) | | is_object ($value)) {

$child = $xml->addchild ($key);

$this->data2xml ($child, $value, $item);

} else {

if (Is_numeric ($value)) {

$child = $xml->addchild ($key, $value);

} else {

$child = $xml->addchild ($key);

$node = Dom_import_simplexml ($child);

$node->appendchild ($node->ownerdocument->createcdatasection ($value));

}

}

}

}

/**

* Signature verification of data to ensure that data is sent

* @param string $token token for open platform settings

* @return Boolean true-signed correctly, false-signature error

*/

Private Function Auth ($token) {

if (Empty ($_get[' signature ')) return;

/* Get Data */

$data = Array ($_get[' timestamp '), $_get[' nonce '], $token);

$sign = $_get[' signature ');

/* Sort the data in a dictionary */

Sort ($data, sort_string);

/* Generate signature */

$signature = SHA1 (implode ($data));

return $signature = = = $sign;

}

}

http://www.bkjia.com/PHPjc/977616.html www.bkjia.com true http://www.bkjia.com/PHPjc/977616.html techarticle PHP Public Platform Development class instance thinkwechat.php class files are 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 29 30 31 The 4 of the ...

  • 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.