您好,登錄后才能下訂單哦!
簡答的自動回復消息完成了,有很多人都不知道關注自動發送給用戶消息是怎么實現的,那么我今天分享一下關注成功后自動發送消息的實現。
看到微信api里面也有介紹到事件推送,那么這個關注事件是如何使用的呢? 今天不廢話,直接上代碼:
<?php define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsg(); class wechatCallbackapiTest { public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if(!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch($RX_TYPE) { case "text" : $resultStr = $this->receiveText($postObj); break; case "event" : $resultStr = $this->receiveEvent($postObj); break; default : $resultStr = "unknow msg type: " . $RX_TYPE; break; } echo $resultStr; } else { echo ""; exit(); } } private function receiveText($object) { if (!empty($object)){ $fromUsername = $object->FromUserName; $toUsername = $object->ToUserName; $keyword = trim($object->Content); $time = time(); //自動回復圖文消息 $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>3</ArticleCount> <Articles> <item> <Title><![CDATA[測試標題1]]></Title> <Description><![CDATA[測試內容啊1!]]></Description> <PicUrl><![CDATA[https://cache.yisu.com/upload/information/20200302/44/7084.jpg]]></PicUrl> <Url><![CDATA[http://www.baidu.com]]></Url> </item> <item> <Title><![CDATA[哈哈,標題]]></Title> <Description><![CDATA[內容標題]]></Description> <PicUrl><![CDATA[https://cache.yisu.com/upload/information/20200302/44/7085.jpg]]></PicUrl> <Url><![CDATA[http://www.discuz.com]]></Url> </item> <item> <Title><![CDATA[12345哈哈,標題]]></Title> <Description><![CDATA[798465789內容標題]]></Description> <PicUrl><![CDATA[https://cache.yisu.com/upload/information/20200302/44/7084.jpg]]></PicUrl> <Url><![CDATA[http://www.google.com]]></Url> </item> </Articles> <FuncFlag>1</FuncFlag> </xml> "; if(!empty( $keyword )){ $msgType = "news"; //類型 news:圖文消息、text:文本消息 event:事件 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function receiveEvent($object) { $contentStr = ""; switch($object->Event) { case "subscribe" : $contentStr = "歡迎關注社區管家!我們可以常聯系了!!"; break; } $resultStr = $this->transmitText($object, $contentStr); return $resultStr; } private function transmitText($object, $content, $flag = 0) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; } } ?>
如果有的童鞋不明白的話,可以留言給我! 我會不定期給大家解決問題!
歡迎留言
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。