网站地图    收藏   

主页 > 后端 > 微信开发 >

方倍微信公众平台开发接口PHP SDK完整版 - 微信公

来源:自学PHP网    时间:2015-04-14 12:58 作者: 阅读:

[导读] 官方提供的SDK只有一个文本消息功能,我们将所有消息的消息类型及事件响应都整理了进来,并且加入日志记录,代码如下:更新日志:2013-01-01 版本1 02014-03-15 增加图片、视频、语音的...

官方提供的SDK只有一个文本消息功能,我们将所有消息的消息类型及事件响应都整理了进来,并且加入日志记录,代码如下:

 

更新日志:

2013-01-01 版本1.0
2014-03-15 增加图片、视频、语音的内容回复
2014-04-09 增加菜单链接事件
2014-04-10 修改文本回复的判定方法
2014-05-20 增加高级群发消息通知事件
2014-05-26 增加多客服消息及多客服的判定方法
2014-05-27 修改自动回复判定方式

 

 

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

 

 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论