官方提供的SDK只有一个文本消息功能,我们将所有消息的消息类型及事件响应都整理了进来,并且加入日志记录,代码如下:
更新日志:
2013-01-01 版本1.0,包含Token验证及基本消息接口的收发2014-03-15 增加图片、视频、语音的内容回复2014-04-09 增加菜单链接事件2014-04-10 修改文本回复的判定方法2014-05-20 增加高级群发消息通知事件2014-05-26 增加多客服消息及多客服的判定方法2014-05-27 修改自动回复判定方式2014-06-20 修复多图文回复的Bug2014-07-10 增加第三方接口处理样式2014-08-02 增加Emoji表格的回复处理2014-10-01 增加自定义菜单扫一扫、发图片、发地理位置等接口的处理2014-10-25 增加消息体签名及加解密的支持2014-11-07 增加该公众号暂时无法提供服务请稍后再试的兼容2014-12-20 移除高级群发消息通知事件,必要性不大2015-02-23 移除消息体签名及加解密的支持,必要性不大2015-04-07 优化客服模式和自动回复模式的判定2015-05-16 优化日志记录,兼容SAE和自有主机
1 <?php 2 /* 3 方倍工作室 http://www.fangbei.org/ 4 CopyRight 2015 All Rights Reserved 5 */ 6 7 define("TOKEN", "weixin"); 8 9 $wechatObj = new wechatCallbackapiTest(); 10 if (!isset($_GET['echostr'])) { 11 $wechatObj->responseMsg(); 12 }else{ 13 $wechatObj->valid(); 14 } 15 16 class wechatCallbackapiTest 17 { 18 //验证签名 19 public function valid() 20 { 21 $echoStr = $_GET["echostr"]; 22 $signature = $_GET["signature"]; 23 $timestamp = $_GET["timestamp"]; 24 $nonce = $_GET["nonce"]; 25 $token = TOKEN; 26 $tmpArr = array($token, $timestamp, $nonce); 27 sort($tmpArr, SORT_STRING); 28 $tmpStr = implode($tmpArr); 29 $tmpStr = sha1($tmpStr); 30 if($tmpStr == $signature){ 31 echo $echoStr; 32 exit; 33 } 34 } 35 36 //响应消息 37 public function responseMsg() 38 { 39 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 40 if (!empty($postStr)){ 41 $this->logger("R \r\n".$postStr); 42 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 43 $RX_TYPE = trim($postObj->MsgType); 44 45 if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")){ 46 //过滤关注和取消关注事件 47 }else{ 48 49 } 50 51 //消息类型分离 52 switch ($RX_TYPE) 53 { 54 case "event": 55 $result = $this->receiveEvent($postObj); 56 break; 57 case "text": 58 if (strstr($postObj->Content, "第三方")){ 59 $result = $this->relayPart3("http://www.fangbei.org/test.php".'?'.$_SERVER['QUERY_STRING'], $postStr); 60 }else{ 61 $result = $this->receiveText($postObj); 62 } 63 break; 64 case "image": 65 $result = $this->receiveImage($postObj); 66 break; 67 case "location": 68 $result = $this->receiveLocation($postObj); 69 break; 70 case "voice": 71 $result = $this->receiveVoice($postObj); 72 break; 73 case "video": 74 $result = $this->receiveVideo($postObj); 75 break; 76 case "link": 77 $result = $this->receiveLink($postObj); 78 break; 79 default: 80 $result = "unknown msg type: ".$RX_TYPE; 81 break; 82 } 83 $this->logger("T \r\n".$result); 84 echo $result; 85 }else { 86 echo ""; 87 exit; 88 } 89 } 90 91 //接收事件消息 92 PRivate function receiveEvent($object) 93 { 94 $content = ""; 95 switch ($object->Event) 96 { 97 case "subscribe": 98 $content = "欢迎关注方倍工作室 "; 99 $content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):"";100 break;101 case "unsubscribe":102 $content = "取消关注";103 break;104 case "CLICK":105 switch ($object->EventKey)106 {107 case "COMPANY":108 $content = array();109 $content[] = array("Title"=>"方倍工作室", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");110 break;111 default:112 $content = "点击菜单:".$object->EventKey;113 break;114 }115 break;116 case "VIEW":117 $content = "跳转链接 ".$object->EventKey;118 break;119 case "SCAN":120 $content = "扫描场景 ".$object->EventKey;121 break;122 case "LOCATION":123 $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;124 break;125 case "scancode_waitmsg":126 if ($object->ScanCodeInfo->ScanType == "qrcode"){127 $content = "扫码带提示:类型 二维码 结果:".$object->ScanCodeInfo->ScanResult;128 }else if ($object->ScanCodeInfo->ScanType == "barcode"){129 $codeinfo = explode(",",strval($object->ScanCodeInfo->ScanResult));130 $codeValue = $codeinfo[1];131 $content = "扫码带提示:类型 条形码 结果:".$codeValue;132 }else{133 $content = "扫码带提示:类型 ".$object->ScanCodeInfo->ScanType." 结果:".$object->ScanCodeInfo->ScanResult;134 }135 break;136 case "scancode_push":137 $content = "扫码推事件";138 break;139 case "pic_sysphoto":140 $content = "系统拍照";141 break;142 case "pic_weixin":143 $content = "相册发图:数量 ".$object->SendPicsInfo->Count;144 break;145 case "pic_photo_or_album":146 $content = "拍照或者相册:数量 ".$object->SendPicsInfo->Count;147 break;148 case "location_select":149 $content = "发送位置:标签 ".$object->SendLocationInfo->Label;150 break;151 default:152 $content = "receive a new event: ".$object->Event;153 break;154 }155 156 if(is_array($content)){157 if (isset($content[0]['PicUrl'])){158 $result = $this->transmitNews($object, $content);159 }else if (isset($content['MusicUrl'])){160 $result = $this->transmitMusic($object, $content);161 }162 }else{163 $result = $this->transmitText($object, $content);164 }165 return $result;166 }167 168 //接收文本消息169 private function receiveText($object)170 {171 $keyWord = trim($object->Content);172 //多客服人工回复模式173 if (strstr($keyword, "请问在吗") || strstr($keyword, "在线客服")){174 $result = $this->transmitService($object);175 return $result;176 }177 178 //自动回复模式179 if (strstr($keyword, "文本")){180 $content = "这是个文本消息";181 }else if (strstr($keyword, "表情")){182 $content = "中国:".$this->bytes_to_emoji(0x1F1E8).$this->bytes_to_emoji(0x1F1F3)."\n仙人掌:".$this->bytes_to_emoji(0x1F335);183 }else if (strstr($keyword, "单图文")){184 $content = array();185 $content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");186 }else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){187 $content = array();188 $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");189