php微信公眾平台開發類執行個體_PHP教程

來源:互聯網
上載者:User

php公眾平台開發類執行個體


ThinkWechat.php類檔案如下:

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

class Wechat {

/**

* 推送過來的資料或響應資料

* @var array

*/

private $data = array();

/**

* 構造方法,用於執行個體化SDK

* @param string $token 開放平台設定的TOKEN

*/

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){

}

}

}

/**

* 擷取推送的資料

* @return array 轉換為數組後的資料

*/

public function request(){

return $this->data;

}

/**

* * 響應發送的資訊(自動回複)

* @param string $to 接收使用者名稱

* @param string $from 寄件者使用者名稱

* @param array $content 回複資訊,文本資訊為string類型

* @param string $type 訊息類型

* @param string $flag 是否新標剛接受到的資訊

* @return string XML字串

*/

public function response($content, $type = 'text', $flag = 0){

/* 基礎資料 */

$this->data = array(

'ToUserName' => $this->data['FromUserName'],

'FromUserName' => $this->data['ToUserName'],

'CreateTime' => time(),

'MsgType' => $type,

);

/* 添加類型資料 */

$this->$type($content);

/* 添加狀態 */

$this->data['FuncFlag'] = $flag;

/* 轉換資料為XML */

$xml = new SimpleXMLElement('');

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

exit($xml->asXML());

}

/**

* 回複文本資訊

* @param string $content 要回複的資訊

*/

private function text($content){

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

}

/**

* 回複音樂資訊

* @param string $content 要回複的音樂

*/

private function music($music){

list(

$music['Title'],

$music['Description'],

$music['MusicUrl'],

$music['HQMusicUrl']

) = $music;

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

}

/**

* 回複圖文資訊

* @param string $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; } //最多隻允許10調新聞

}

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

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

}

/**

* 資料XML編碼

* @param object $xml XML對象

* @param mixed $data 資料

* @param string $item 數字索引時的節點名稱

* @return string

*/

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

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

/* 指定預設的數字key */

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

/* 添加子項目 */

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));

}

}

}

}

/**

* 對資料進行簽名認證,確保是發送的資料

* @param string $token 開放平台設定的TOKEN

* @return boolean true-簽名正確,false-簽名錯誤

*/

private function auth($token){

if(empty($_GET['signature'])) return;

/* 擷取資料 */

$data = array($_GET['timestamp'], $_GET['nonce'], $token);

$sign = $_GET['signature'];

/* 對資料進行字典排序 */

sort($data,SORT_STRING);

/* 產生簽名 */

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

return $signature === $sign;

}

}

http://www.bkjia.com/PHPjc/977616.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/977616.htmlTechArticlephp公眾平台開發類執行個體 ThinkWechat.php類檔案如下: 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 4...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.