php微信開發(fā)之關(guān)鍵詞回復(fù)功能
本文實(shí)例為大家分享了php微信開發(fā)之關(guān)鍵詞回復(fù)的具體代碼,供大家參考,具體內(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; $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(!empty( $keyword )) { $msgType = "text"; if($keyword=="1"){ $contentStr = "qiphon";} if($keyword=="2"){ $contentStr = "最新資訊:《環(huán)境違法行為舉報(bào)獎(jiǎng)勵(lì)辦法》";} if($keyword=="3"){ $contentStr = "31離婚協(xié)議\n32租房合同\n33交通事故協(xié)議書";} $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; } } } ?>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php生成縮略圖示例代碼分享(使用gd庫實(shí)現(xiàn))
分享一個(gè)利用php的GD庫生成縮略圖的例子,大家參考使用吧2014-01-01thinkphp5.1框架中容器(Container)和門面(Facade)的實(shí)現(xiàn)方法分析
這篇文章主要介紹了thinkphp5.1框架中容器(Container)和門面(Facade)的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkPHP5.1框架中容器與門面的定義、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08Thinkphp5框架簡單實(shí)現(xiàn)鉤子(Hook)行為的方法示例
這篇文章主要介紹了Thinkphp5框架簡單實(shí)現(xiàn)鉤子(Hook)行為的方法,結(jié)合實(shí)例形式詳細(xì)分析了Thinkphp5框架實(shí)現(xiàn)鉤子(Hook)行為的步驟與相關(guān)操作技巧,需要的朋友可以參考下2019-09-09利用php實(shí)現(xiàn)一周之內(nèi)自動(dòng)登錄存儲(chǔ)機(jī)制(cookie、session、localStorage)
本篇文章主要介紹了利用php實(shí)現(xiàn)一周之內(nèi)自動(dòng)登錄存儲(chǔ)機(jī)制(cookie、session、localStorage),可以實(shí)現(xiàn)一周之內(nèi)免費(fèi)登陸,感興趣的小伙伴們可以參考一下。2016-10-10php redis 處理websocket聊天記錄的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了php redis 處理websocket聊天記錄的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07PHP 實(shí)現(xiàn)從數(shù)據(jù)庫導(dǎo)出到.csv文件方法
這篇文章主要介紹了 PHP 實(shí)現(xiàn)從數(shù)據(jù)庫導(dǎo)出到.csv文件方法的相關(guān)資料,需要的朋友可以參考下2017-07-07