Php WeChat public platform development example

Source: Internet
Author: User

Php public platform development example

The ThinkWechat. php 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

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

<? Php

Class Wechat {

/**

* Pushed data or response data

* @ Var array

*/

Private $ data = array ();

/**

* Constructor, used to instantiate the SDK

* @ Param string $ token the TOKEN set for the token Open Platform

*/

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 the pushed data

* @ Return the data after array Conversion

*/

Public function request (){

Return $ this-> data;

}

/**

** Response Message (automatic reply)

* @ Param string $ to receive the user name

* @ Param string $ from the sender's username

* @ Param array $ content: the reply message. The text information is of the string type.

* @ Param string $ type Message type

* @ Param string $ flag indicates whether the new logo has just received the 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 the data to XML */

$ Xml = new SimpleXMLElement ('<xml> </xml> ');

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

Exit ($ xml-> asXML ());

}

/**

* Reply to text information

* @ Param string $ content the information to be replied

*/

Private function text ($ content ){

$ This-> data ['content'] = $ Content;

}

/**

* Reply to music information

* @ Param string $ the music to be replied to by content

*/

Private function music ($ music ){

List (

$ Music ['title'],

$ Music ['description'],

$ Music ['musicurl'],

$ Music ['hqmusicurl']

) = $ Music;

$ This-> data ['music'] = $ Music;

}

/**

* Reply to text information

* @ Param string $ text content to be replied to by news

*/

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;} // a maximum of 10 news calls are allowed.

}

$ This-> data ['articlescount'] = count ($ articles );

$ This-> data ['articles '] = $ Articles;

}

/**

* Data XML Encoding

* @ Param object $ xml XML object

* @ Param mixed $ data

* @ Param string $ the node name of the item numeric index

* @ Return string

*/

Private function data2xml ($ xml, $ data, $ item = 'item '){

Foreach ($ data as $ key => $ value ){

/* Specify the default numeric key */

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

/* Add sub-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 ));

}

}

}

}

/**

* Sign and authenticate the data to ensure that the data is sent.

* @ Param string $ token the TOKEN set for the token Open Platform

* @ Return boolean true-correct signature, false-Incorrect Signature

*/

Private function auth ($ token ){

If (empty ($ _ GET ['signature']) return;

/* Get Data */

$ Data = array ($ _ GET ['timestamp'], $ _ GET ['nonce '], $ token );

$ Sign = $ _ GET ['signature'];

/* Sort data alphabetically */

Sort ($ data, SORT_STRING );

/* Generate a signature */

$ Signature = sha1 (implode ($ data ));

Return $ signature ===$ sign;

}

}

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.