程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 微信公眾號開發之文本消息自動回復,公眾自動回復

微信公眾號開發之文本消息自動回復,公眾自動回復

編輯:關於PHP編程

微信公眾號開發之文本消息自動回復,公眾自動回復


1.PHP示例代碼下載

下載地址1:http://pan.baidu.com/s/1nvlhbnV、

下載地址2:https://mp.weixin.qq.com/wiki/home/index.html(開始開發-》接入指南-》PHP示例代碼下載)

2.wx_sample.php初始代碼

1 <?php 2 /** 3 * wechat php test 4 */ 5 6 //define your token 7 define("TOKEN", "weixin"); 8 $wechatObj = new wechatCallbackapiTest(); 9 $wechatObj->valid(); 10 11 class wechatCallbackapiTest 12 { 13 public function valid() 14 { 15 $echoStr = $_GET["echostr"]; 16 17 //valid signature , option 18 if($this->checkSignature()){ 19 echo $echoStr; 20 exit; 21 } 22 } 23 24 public function responseMsg() 25 { 26 //get post data, May be due to the different environments 27 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 28 29 //extract post data 30 if (!empty($postStr)){ 31 /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, 32 the best way is to check the validity of xml by yourself */ 33 libxml_disable_entity_loader(true); 34 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 35 $fromUsername = $postObj->FromUserName; 36 $toUsername = $postObj->ToUserName; 37 $keyword = trim($postObj->Content); 38 $time = time(); 39 $textTpl = "<xml> 40 <ToUserName><![CDATA[%s]]></ToUserName> 41 <FromUserName><![CDATA[%s]]></FromUserName> 42 <CreateTime>%s</CreateTime> 43 <MsgType><![CDATA[%s]]></MsgType> 44 <Content><![CDATA[%s]]></Content> 45 <FuncFlag>0</FuncFlag> 46 </xml>"; 47 if(!empty( $keyword )) 48 { 49 $msgType = "text"; 50 $contentStr = "Welcome to wechat world!"; 51 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 52 echo $resultStr; 53 }else{ 54 echo "Input something..."; 55 } 56 57 }else { 58 echo ""; 59 exit; 60 } 61 } 62 63 private function checkSignature() 64 { 65 // you must define TOKEN by yourself 66 if (!defined("TOKEN")) { 67 throw new Exception('TOKEN is not defined!'); 68 } 69 70 $signature = $_GET["signature"]; 71 $timestamp = $_GET["timestamp"]; 72 $nonce = $_GET["nonce"]; 73 74 $token = TOKEN; 75 $tmpArr = array($token, $timestamp, $nonce); 76 // use SORT_STRING rule 77 sort($tmpArr, SORT_STRING); 78 $tmpStr = implode( $tmpArr ); 79 $tmpStr = sha1( $tmpStr ); 80 81 if( $tmpStr == $signature ){ 82 return true; 83 }else{ 84 return false; 85 } 86 } 87 } 88 89 ?> wx_sample.php

3.調用回復信息方法

在wx_sample.php文件中注釋掉$wechatObj->valid();,在其下增加一句“$wechatObj->responseMsg();”。

1 <?php 2 /** 3 * wechat php test 4 */ 5 6 //define your token 7 define("TOKEN", "weixin"); 8 $wechatObj = new wechatCallbackapiTest(); 9 //$wechatObj->valid();//接口驗證 10 $wechatObj->responseMsg();//調用回復消息方法 11 class wechatCallbackapiTest 12 { 13 public function valid() 14 { 15 $echoStr = $_GET["echostr"]; 16 17 //valid signature , option 18 if($this->checkSignature()){ 19 echo $echoStr; 20 exit; 21 } 22 } 23 24 public function responseMsg() 25 { 26 //get post data, May be due to the different environments 27 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 28 29 //extract post data 30 if (!empty($postStr)){ 31 /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, 32 the best way is to check the validity of xml by yourself */ 33 libxml_disable_entity_loader(true); 34 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 35 $fromUsername = $postObj->FromUserName; 36 $toUsername = $postObj->ToUserName; 37 $keyword = trim($postObj->Content); 38 $time = time(); 39 $textTpl = "<xml> 40 <ToUserName><![CDATA[%s]]></ToUserName> 41 <FromUserName><![CDATA[%s]]></FromUserName> 42 <CreateTime>%s</CreateTime> 43 <MsgType><![CDATA[%s]]></MsgType> 44 <Content><![CDATA[%s]]></Content> 45 <FuncFlag>0</FuncFlag> 46 </xml>"; 47 if(!empty( $keyword )) 48 { 49 $msgType = "text"; 50 $contentStr = "Welcome to wechat world!"; 51 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 52 echo $resultStr; 53 }else{ 54 echo "Input something..."; 55 } 56 57 }else { 58 echo ""; 59 exit; 60 } 61 } 62 63 private function checkSignature() 64 { 65 // you must define TOKEN by yourself 66 if (!defined("TOKEN")) { 67 throw new Exception('TOKEN is not defined!'); 68 } 69 70 $signature = $_GET["signature"]; 71 $timestamp = $_GET["timestamp"]; 72 $nonce = $_GET["nonce"]; 73 74 $token = TOKEN; 75 $tmpArr = array($token, $timestamp, $nonce); 76 // use SORT_STRING rule 77 sort($tmpArr, SORT_STRING); 78 $tmpStr = implode( $tmpArr ); 79 $tmpStr = sha1( $tmpStr ); 80 81 if( $tmpStr == $signature ){ 82 return true; 83 }else{ 84 return false; 85 } 86 } 87 } 88 89 ?> 調用回復信息方法

4.關鍵詞自動回復和關注回復

$keyword保存著用戶微信端發來的文本信息。

官方開發者文檔:https://mp.weixin.qq.com/wiki/home/index.html(消息管理-》接收消息-接收事件推送-》1.關注/取消關注事件)

    關注事件與一般的文本消息有兩處不同,一是MsgType值是event,二是增加了Event值是subscribe。由於官方文檔(最初的wx_sample.php)沒有提取這個參數,需要我們自己提取。在程序中增加兩個變量$msgType和$event。

1 <?php 2 /** 3 * wechat php test 4 */ 5 6 //define your token 7 define("TOKEN", "weixin"); 8 $wechatObj = new wechatCallbackapiTest(); 9 //$wechatObj->valid();//接口驗證 10 $wechatObj->responseMsg();//調用回復消息方法 11 class wechatCallbackapiTest 12 { 13 public function valid() 14 { 15 $echoStr = $_GET["echostr"]; 16 17 //valid signature , option 18 if($this->checkSignature()){ 19 echo $echoStr; 20 exit; 21 } 22 } 23 24 public function responseMsg() 25 { 26 //get post data, May be due to the different environments 27 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 28 29 //extract post data 30 if (!empty($postStr)){ 31 /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, 32 the best way is to check the validity of xml by yourself */ 33 libxml_disable_entity_loader(true); 34 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 35 $fromUsername = $postObj->FromUserName; 36 $toUsername = $postObj->ToUserName; 37 $keyword = trim($postObj->Content); 38 $time = time(); 39 $msgType = $postObj->MsgType;//消息類型 40 $event = $postObj->Event;//時間類型,subscribe(訂閱)、unsubscribe(取消訂閱) 41 $textTpl = "<xml> 42 <ToUserName><![CDATA[%s]]></ToUserName> 43 <FromUserName><![CDATA[%s]]></FromUserName> 44 <CreateTime>%s</CreateTime> 45 <MsgType><![CDATA[%s]]></MsgType> 46 <Content><![CDATA[%s]]></Content> 47 <FuncFlag>0</FuncFlag> 48 </xml>"; 49 50 switch($msgType){ 51 case "event": 52 if($event=="subscribe"){ 53 $contentStr = "Hi,歡迎關注海仙日用百貨!"."\n"."回復數字'1',了解店鋪地址."."\n"."回復數字'2',了解商品種類."; 54 } 55 break; 56 case "text": 57 switch($keyword){ 58 case "1": 59 $contentStr = "店鋪地址:"."\n"."杭州市江干艮山西路233號新東升市場地下室第一排."; 60 break; 61 case "2": 62 $contentStr = "商品種類:"."\n"."杯子、碗、棉簽、水桶、垃圾桶、洗碗巾(刷)、拖把、掃把、" 63 ."衣架、粘鉤、牙簽、垃圾袋、保鮮袋(膜)、剪刀、水果刀、飯盒等."; 64 break; 65 default: 66 $contentStr = "對不起,你的內容我會稍後回復"; 67 } 68 break; 69 } 70 $msgType = "text"; 71 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 72 echo $resultStr; 73 }else { 74 echo ""; 75 exit; 76 } 77 } 78 79 private function checkSignature() 80 { 81 // you must define TOKEN by yourself 82 if (!defined("TOKEN")) { 83 throw new Exception('TOKEN is not defined!'); 84 } 85 86 $signature = $_GET["signature"]; 87 $timestamp = $_GET["timestamp"]; 88 $nonce = $_GET["nonce"]; 89 90 $token = TOKEN; 91 $tmpArr = array($token, $timestamp, $nonce); 92 // use SORT_STRING rule 93 sort($tmpArr, SORT_STRING); 94 $tmpStr = implode( $tmpArr ); 95 $tmpStr = sha1( $tmpStr ); 96 97 if( $tmpStr == $signature ){ 98 return true; 99 }else{ 100 return false; 101 } 102 } 103 } 104 105 106 ?> 關鍵詞回復和關注回復

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved