本文執行個體為大家分享了php簡訊自動回複 別代碼,供大家參考,具體內容如下
1.PHP範例程式碼下載
下載地址1:http://xiazai.php.net/201608/yuanma/phpwx(php.net).rar
下載地址2:https://mp.weixin.qq.com/wiki/home/index.html(開始開發-》接入指南-》PHP範例程式碼下載)
2.wx_sample.php初始代碼
valid();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)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "%s0"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>
3.調用回複資訊方法
在wx_sample.php檔案中注釋掉$wechatObj->valid();,在其下增加一句“$wechatObj->responseMsg();”。
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)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "%s0"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>
4.關鍵詞自動回複和關注回複
$keyword儲存著使用者端發來的文本資訊。
官方開發人員文檔:https://mp.weixin.qq.com/wiki/home/index.html(訊息管理-》接收訊息-接收事件推送-》1.關注/取消追蹤事件)
關注事件與一般的簡訊有兩處不同,一是MsgType值是event,二是增加了Event值是subscribe。由於官方文檔(最初的wx_sample.php)沒有提取這個參數,需要我們自己提取。在程式中增加兩個變數$msgType和$event。
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)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $msgType = $postObj->MsgType;//訊息類型 $event = $postObj->Event;//時間類型,subscribe(訂閱)、unsubscribe(取消訂閱) $textTpl = "%s0"; switch($msgType){ case "event": if($event=="subscribe"){ $contentStr = "Hi,歡迎關注海仙日用百貨!"."\n"."回複數字'1',瞭解店鋪地址."."\n"."回複數字'2',瞭解商品種類."; } break; case "text": switch($keyword){ case "1": $contentStr = "店鋪地址:"."\n"."杭州市江幹艮山西路233號新東升市場地下室第一排."; break; case "2": $contentStr = "商品種類:"."\n"."杯子、碗、棉簽、水桶、垃圾桶、洗碗巾(刷)、拖把、掃把、" ."衣架、粘鉤、牙籤、垃圾袋、保鮮袋(膜)、剪刀、水果刀、飯盒等."; break; default: $contentStr = "對不起,你的內容我會稍後回複"; } break; } $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>
以上就是公眾號開發之簡訊自動回複php代碼_php執行個體的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!