-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwechat.php
153 lines (128 loc) · 3.78 KB
/
wechat.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
143
144
145
146
147
148
149
150
151
152
153
<?php
/*
File: wechat.php
Author: [email protected]
Date: 2012.11.30
Usage: api封装
Comment: 详见readme.txt
*/
class Wechat
{
//似乎没什么用,放着用来自动完成吧。
static $req_keys = array( "Content", "CreateTime", "FromUserName", "Label",
"Location_X", "Location_Y", "MsgType", "PicUrl", "Scale", "ToUserName", );
public $token;
public $request = array();
protected $funcflag = false;
protected $debug = false;
public function __construct($token, $debug = false)
{
$this->token = $token;
$this->debug = $debug;
}
public function get_msg_type()
{
return strtolower($this->request['MsgType']);
}
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function set_funcflag()
{
$this->funcflag = true;
}
public function replyText($message)
{
$textTpl = <<<eot
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>
eot;
$req = $this->request;
return sprintf($textTpl, $req['FromUserName'], $req['ToUserName'],
time(), 'text', $message, $this->funcflag ? 1 : 0);
}
public function replyNews($arr_item)
{
$itemTpl = <<<eot
<item>
<Title><![CDATA[%s]]></Title>
<Discription><![CDATA[%s]]></Discription>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
eot;
$real_arr_item = $arr_item;
if (isset($arr_item['title']))
$real_arr_item = array($arr_item);
$nr = count($real_arr_item);
$item_str = "";
foreach ($real_arr_item as $item)
$item_str .= sprintf($itemTpl, $item['title'], $item['description'],
$item['pic'], $item['url']);
$time = time();
$fun = $this->funcflag ? 1 : 0;
return <<<eot
<xml>
<ToUserName><![CDATA[{$this->request['FromUserName']}]]></ToUserName>
<FromUserName><![CDATA[{$this->request['ToUserName']}]]></FromUserName>
<CreateTime>{$time}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>{$nr}</ArticleCount>
<Articles>
$item_str
</Articles>
<FuncFlag>{$fun}</FuncFlag>
</xml>
eot;
}
public function reply($callback)
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if ($this->debug)
file_put_contents("request.txt", $postStr);
if(empty($postStr) || !$this->checkSignature())
die("bad request");
$this->request = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$arg = call_user_func($callback, $this->request, $this);
if (!is_array($arg))
$ret = $this->replyText($arg);
else
$ret = $this->replyNews($arg);
if ($this->debug)
file_put_contents("response.txt", $ret);
echo $ret;
}
private function checkSignature()
{
$args = array("signature", "timestamp", "nonce");
foreach ($args as $arg)
if (!isset($_GET[$arg]))
return false;
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$tmpArr = array($this->token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>