您好,登錄后才能下訂單哦!
簡答的自動回復(fù)消息完成了,有很多人都不知道關(guān)注自動發(fā)送給用戶消息是怎么實(shí)現(xiàn)的,那么我今天分享一下關(guān)注成功后自動發(fā)送消息的實(shí)現(xiàn)。
看到微信api里面也有介紹到事件推送,那么這個關(guān)注事件是如何使用的呢? 今天不廢話,直接上代碼:
<?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(); //自動回復(fù)圖文消息 $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>3</ArticleCount> <Articles> <item> <Title><![CDATA[測試標(biāo)題1]]></Title> <Description><![CDATA[測試內(nèi)容啊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[哈哈,標(biāo)題]]></Title> <Description><![CDATA[內(nèi)容標(biāo)題]]></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哈哈,標(biāo)題]]></Title> <Description><![CDATA[798465789內(nèi)容標(biāo)題]]></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 = "歡迎關(guān)注社區(qū)管家!我們可以常聯(lián)系了??!"; 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; } } ?>
如果有的童鞋不明白的話,可以留言給我! 我會不定期給大家解決問題!
歡迎留言
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。