php微信開(kāi)發(fā)之關(guān)注事件
本文實(shí)例為大家分享了php微信關(guān)注事件的具體代碼,供大家參考,具體內(nèi)容如下
<?php /** * wechat php test */ //define your token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); //$wechatObj->valid(); $wechatObj->responseMsg(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $type = $postObj->MsgType; $customevent = $postObj->Event; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if($type=="event" and $customrevent=="subscribe"){ $contentStr = "感謝你的關(guān)注\n回復(fù)1查看聯(lián)系方式\n回復(fù)2查看最新資訊\n回復(fù)3查看法律文書"; $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } if(!empty( $keyword )) { $msgType = "text"; if($keyword=="1"){ $contentStr = "qiphon";} if($keyword=="2"){ $contentStr = "test 。";} if($keyword=="3"){ $contentStr = "test333";} $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法分析
這篇文章主要介紹了Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法,結(jié)合實(shí)例形式分析了srbac模塊的原理及權(quán)限操作的相關(guān)技巧,需要的朋友可以參考下2016-07-07合并ThinkPHP配置文件以消除代碼冗余的實(shí)現(xiàn)方法
這篇文章主要介紹了合并ThinkPHP配置文件以消除代碼冗余的實(shí)現(xiàn)方法,非常實(shí)用的技巧,需要的朋友可以參考下2014-07-07php實(shí)現(xiàn)smarty模板無(wú)限極分類的方法
這篇文章主要介紹了php實(shí)現(xiàn)smarty模板無(wú)限極分類的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了php使用smarty模板實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢與無(wú)限極分類的相關(guān)技巧,需要的朋友可以參考下2015-12-12PHP中使用register_shutdown_function函數(shù)截獲fatal error示例
這篇文章主要介紹了PHP中使用register_shutdown_function函數(shù)截獲fatal error示例,需要的朋友可以參考下2015-04-04Yii+MYSQL鎖表防止并發(fā)情況下重復(fù)數(shù)據(jù)的方法
這篇文章主要介紹了Yii+MYSQL鎖表防止并發(fā)情況下重復(fù)數(shù)據(jù)的方法,分析了MySQL讀鎖定、寫鎖定及Yii實(shí)現(xiàn)MySQL讀寫鎖定的相關(guān)技巧,需要的朋友可以參考下2016-07-07PHP不用遞歸實(shí)現(xiàn)無(wú)限分級(jí)的例子分享
這篇文章主要介紹了PHP不用遞歸實(shí)現(xiàn)無(wú)限分級(jí)的例子,實(shí)際上是一種思路,并用簡(jiǎn)單的例子來(lái)說(shuō)明,需要的朋友可以參考下2014-04-04