1 <?php
2
3
4 define("TOKEN", "weixin");
5
6 $wechatObj = new wechatCallbackapiTest();
7 if (!isset($_GET[‘echostr‘])) {
8 $wechatObj->responseMsg();
9 }else{
10 $wechatObj->valid();
11 }
12
13 class wechatCallbackapiTest
14 {
15 public function valid()
16 {
17 $echoStr = $_GET["echostr"];
18 if($this->checkSignature()){
19 echo $echoStr;
20 exit;
21 }
22 }
23
24 private function checkSignature()
25 {
26 $signature = $_GET["signature"];
27 $timestamp = $_GET["timestamp"];
28 $nonce = $_GET["nonce"];
29 $token = TOKEN;
30 $tmpArr = array($token, $timestamp, $nonce);
31 sort($tmpArr, SORT_STRING);
32 $tmpStr = implode($tmpArr);
33 $tmpStr = sha1($tmpStr);
34
35 if($tmpStr == $signature){
36 return true;
37 }else{
38 return false;
39 }
40 }
41
42 public function responseMsg()
43 {
44 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
45 if (!empty($postStr)){
46 $this->logger("R ".$postStr);
47 $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
48 $RX_TYPE = trim($postObj->MsgType);
49
50 switch ($RX_TYPE)
51 {
52 case "event":
53 $result = $this->receiveEvent($postObj);
54 break;
55 case "text":
56 $result = $this->receiveText($postObj);
57 break;
58 case "image":
59 $result = $this->receiveImage($postObj);
60 break;
61 case "location":
62 $result = $this->receiveLocation($postObj);
63 break;
64 case "voice":
65 $result = $this->receiveVoice($postObj);
66 break;
67 case "video":
68 $result = $this->receiveVideo($postObj);
69 break;
70 case "link":
71 $result = $this->receiveLink($postObj);
72 break;
73 default:
74 $result = "unknow msg type: ".$RX_TYPE;
75 break;
76 }
77 $this->logger("T ".$result);
78 echo $result;
79 }else {
80 echo "";
81 exit;
82 }
83 }
84
85 private function receiveEvent($object)
86 {
87 $content = "";
88 switch ($object->Event)
89 {
90 case "subscribe":
91 $content = "欢迎关注方倍工作室 ";
92 $content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):"";
93 break;
94 case "unsubscribe":
95 $content = "取消关注";
96 break;
97 case "SCAN":
98 $content = "扫描场景 ".$object->EventKey;
99 break;
100 case "CLICK":
101 switch ($object->EventKey)
102 {
103 case "COMPANY":
104 $content = "方倍工作室提供互联网相关产品与服务。";
105 break;
106 default:
107 $content = "点击菜单:".$object->EventKey;
108 break;
109 }
110 break;
111 case "LOCATION":
112 $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;
113 break;
114 default:
115 $content = "receive a new event: ".$object->Event;
116 break;
117 }
118 $result = $this->transmitText($object, $content);
119 return $result;
120 }
121
122 private function receiveText($object)
123 {
124 $keyword = trim($object->Content);
125 switch ($keyword)
126 {
127 case "文本":
128 $content = "这是个文本消息";
129 break;
130 case "图文":
131 case "单图文":
132 $content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
133 break;
134 case "多图文":
135 $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
136 $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
137 $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
138 break;
139 case "音乐":
140 $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");
141 break;
142 default:
143 $content = "当前时间:".date("Y-m-d H:i:s",time());
144 break;
145 }
146 if(is_array($content)){
147 if (isset($content[0][‘PicUrl‘])){
148 $result = $this->transmitNews($object, $content);
149 }else if (isset($content[‘MusicUrl‘])){
150 $result = $this->transmitMusic($object, $content);
151 }
152 }else{
153 $result = $this->transmitText($object, $content);
154 }
155 return $result;
156 }
157
158 private function receiveImage($object)
159 {
160 $content = array("MediaId"=>$object->MediaId);
161 $result = $this->transmitImage($object, $content);
162 return $result;
163 }
164
165 private function receiveLocation($object)
166 {
167 $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
168 $result = $this->transmitText($object, $content);
169 return $result;
170 }
171
172 private function receiveVoice($object)
173 {
174 if (empty($object->Recognition)){
175 $content = array("MediaId"=>$object->MediaId);
176 $result = $this->transmitVoice($object, $content);
177 }else{
178 $content = "你刚才说的是:".$object->Recognition;
179 $result = $this->transmitText($object, $content);
180 }
181
182 return $result;
183 }
184
185 private function receiveVideo($object)
186 {
187 $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");
188 $result = $this->transmitVideo($object, $content);
189 return $result;
190 }
191
192 private function receiveLink($object)
193 {
194 $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
195 $result = $this->transmitText($object, $content);
196 return $result;
197 }
198
199 private function transmitText($object, $content)
200 {
201 $textTpl = "<xml>
202 <ToUserName><![CDATA[%s]]></ToUserName>
203 <FromUserName><![CDATA[%s]]></FromUserName>
204 <CreateTime>%s</CreateTime>
205 <MsgType><![CDATA[text]]></MsgType>
206 <Content><![CDATA[%s]]></Content>
207 </xml>";
208 $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
209 return $result;
210 }
211
212 private function transmitImage($object, $imageArray)
213 {
214 $itemTpl = "<Image>
215 <MediaId><![CDATA[%s]]></MediaId>
216 </Image>";
217
218 $item_str = sprintf($itemTpl, $imageArray[‘MediaId‘]);
219
220 $textTpl = "<xml>
221 <ToUserName><![CDATA[%s]]></ToUserName>
222 <FromUserName><![CDATA[%s]]></FromUserName>
223 <CreateTime>%s</CreateTime>
224 <MsgType><![CDATA[image]]></MsgType>
225 $item_str
226 </xml>";
227
228 $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
229 return $result;
230 }
231
232 private function transmitVoice($object, $voiceArray)
233 {
234 $itemTpl = "<Voice>
235 <MediaId><![CDATA[%s]]></MediaId>
236 </Voice>";
237
238 $item_str = sprintf($itemTpl, $voiceArray[‘MediaId‘]);
239
240 $textTpl = "<xml>
241 <ToUserName><![CDATA[%s]]></ToUserName>
242 <FromUserName><![CDATA[%s]]></FromUserName>
243 <CreateTime>%s</CreateTime>
244 <MsgType><![CDATA[voice]]></MsgType>
245 $item_str
246 </xml>";
247
248 $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
249 return $result;
250 }
251
252 private function transmitVideo($object, $videoArray)
253 {
254 $itemTpl = "<Video>
255 <MediaId><![CDATA[%s]]></MediaId>
256 <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
257 <Title><![CDATA[%s]]></Title>
258 <Description><![CDATA[%s]]></Description>
259 </Video>";
260
261 $item_str = sprintf($itemTpl, $videoArray[‘MediaId‘], $videoArray[‘ThumbMediaId‘], $videoArray[‘Title‘], $videoArray[‘Description‘]);
262
263 $textTpl = "<xml>
264 <ToUserName><![CDATA[%s]]></ToUserName>
265 <FromUserName><![CDATA[%s]]></FromUserName>
266 <CreateTime>%s</CreateTime>
267 <MsgType><![CDATA[video]]></MsgType>
268 $item_str
269 </xml>";
270
271 $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
272 return $result;
273 }
274
275 private function transmitNews($object, $newsArray)
276 {
277 if(!is_array($newsArray)){
278 return;
279 }
280 $itemTpl = " <item>
281 <Title><![CDATA[%s]]></Title>
282 <Description><![CDATA[%s]]></Description>
283 <PicUrl><![CDATA[%s]]></PicUrl>
284 <Url><![CDATA[%s]]></Url>
285 </item>
286 ";
287 $item_str = "";
288 foreach ($newsArray as $item){
289 $item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘], $item[‘Url‘]);
290 }
291 $newsTpl = "<xml>
292 <ToUserName><![CDATA[%s]]></ToUserName>
293 <FromUserName><![CDATA[%s]]></FromUserName>
294 <CreateTime>%s</CreateTime>
295 <MsgType><![CDATA[news]]></MsgType>
296 <Content><![CDATA[]]></Content>
297 <ArticleCount>%s</ArticleCount>
298 <Articles>
299 $item_str</Articles>
300 </xml>";
301
302 $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
303 return $result;
304 }
305
306 private function transmitMusic($object, $musicArray)
307 {
308 $itemTpl = "<Music>
309 <Title><![CDATA[%s]]></Title>
310 <Description><![CDATA[%s]]></Description>
311 <MusicUrl><![CDATA[%s]]></MusicUrl>
312 <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
313 </Music>";
314
315 $item_str = sprintf($itemTpl, $musicArray[‘Title‘], $musicArray[‘Description‘], $musicArray[‘MusicUrl‘], $musicArray[‘HQMusicUrl‘]);
316
317 $textTpl = "<xml>
318 <ToUserName><![CDATA[%s]]></ToUserName>
319 <FromUserName><![CDATA[%s]]></FromUserName>
320 <CreateTime>%s</CreateTime>
321 <MsgType><![CDATA[music]]></MsgType>
322 $item_str
323 </xml>";
324
325 $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
326 return $result;
327 }
328
329 private function logger($log_content)
330 {
331 if(isset($_SERVER[‘HTTP_APPNAME‘])){ //SAE
332 sae_set_display_errors(false);
333 sae_debug($log_content);
334 sae_set_display_errors(true);
335 }else if($_SERVER[‘REMOTE_ADDR‘] != "127.0.0.1"){ //LOCAL
336 $max_size = 10000;
337 $log_filename = "log.xml";
338 if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
339 file_put_contents($log_filename, date(‘H:i:s‘)." ".$log_content."\r\n", FILE_APPEND);
340 }
341 }
342 }
343
344
345 ?>
346
347
348
349
350
351 文章摘自方倍工作室http://www.cnblogs.com/txw1958/p/weixin-php-sdk.html