700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 微信公众号开发之文本消息自动回复

微信公众号开发之文本消息自动回复

时间:2022-10-26 11:50:27

相关推荐

微信公众号开发之文本消息自动回复

php教程|php手册

微信公众号开发之文本消息自动回复

php教程-php手册

1.PHP示例代码下载

篮球网站源码,vscode底层框架,ubuntu修改文件目录名,tomcat僵尸进程,sqlite查看表的,discuz插件开发视频教程,前端页面拖拽设计框架,java爬虫避免登录失效,代码规范php,大邑优化seo,易思企业网站管理,编写网页程序的软件有哪些,html5 模板下载,VB代码下载程序lzw

下载地址1:/s/1nvlhbnV、

学校题库管理系统 c 源码,重新加载ubuntu桌面,tomcat6的开始页面,choice终端爬虫,php的post接口测试,帽seo教程lzw

下载地址2:https://mp./wiki/home/index.html(开始开发-》接入指南-》PHP示例代码下载)

手游捕鱼源码,怎么更改vscode浏览器,配网关ubuntu,linux怎么进tomcat,cookies反爬虫,会php如何创业,深圳长沙seo优化哪家专业lzw

2.wx_sample.php初始代码

1 valid();10 11 class wechatCallbackapiTest12 {13public function valid()14{15 $echoStr = $_GET["echostr"];16 17 //valid signature , option18 if($this->checkSignature()){19 echo $echoStr;20 exit;21 }22}23 24public function responseMsg()25{26 //get post data, May be due to the different environments27 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];28 29 //extract post data30 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 = "40 <![CDATA[%s]]>41 <![CDATA[%s]]>42 %s43 <![CDATA[%s]]>44 <![CDATA[%s]]>45 046 "; 47 if(!empty( $keyword ))48 {49 $msgType = "text";50$contentStr = "Welcome to wechat world!";51$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);52echo $resultStr;53 }else{54echo "Input something...";55 }56 57 }else {58 echo "";59 exit;60 }61}62 63private function checkSignature()64{65 // you must define TOKEN by yourself66 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 rule77 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.php3.调用回复信息方法

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

1 valid();//接口验证10 $wechatObj->responseMsg();//调用回复消息方法11 class wechatCallbackapiTest12 {13public function valid()14{15 $echoStr = $_GET["echostr"];16 17 //valid signature , option18 if($this->checkSignature()){19 echo $echoStr;20 exit;21 }22}23 24public function responseMsg()25{26 //get post data, May be due to the different environments27 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];28 29 //extract post data30 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 = "40 <![CDATA[%s]]>41 <![CDATA[%s]]>42 %s43 <![CDATA[%s]]>44 <![CDATA[%s]]>45 046 "; 47 if(!empty( $keyword ))48 {49 $msgType = "text";50$contentStr = "Welcome to wechat world!";51$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);52echo $resultStr;53 }else{54echo "Input something...";55 }56 57 }else {58 echo "";59 exit;60 }61}62 63private function checkSignature()64{65 // you must define TOKEN by yourself66 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 rule77 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./wiki/home/index.html(消息管理-》接收消息-接收事件推送-》1.关注/取消关注事件)

关注事件与一般的文本消息有两处不同,一是MsgType值是event,二是增加了Event值是subscribe。由于官方文档(最初的wx_sample.php)没有提取这个参数,需要我们自己提取。在程序中增加两个变量$msgType和$event。

1 valid();//接口验证 10 $wechatObj->responseMsg();//调用回复消息方法 11 class wechatCallbackapiTest 12 { 13public function valid() 14{ 15 $echoStr = $_GET["echostr"]; 16 17 //valid signature , option 18 if($this->checkSignature()){ 19 echo $echoStr; 20 exit; 21 } 22} 23 24public 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 = " 42 <![CDATA[%s]]> 43 <![CDATA[%s]]> 44 %s 45 <![CDATA[%s]]> 46 <![CDATA[%s]]> 47 0 48 "; 49 50 switch($msgType){ 51case "event": 52if($event=="subscribe"){ 53 $contentStr = "Hi,欢迎关注海仙日用百货!"."\n"."回复数字1,了解店铺地址."."\n"."回复数字2,了解商品种类."; 54} 55break; 56case "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 } 68break; 69 } 70 $msgType = "text"; 71 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 72 echo $resultStr; 73 }else { 74 echo ""; 75 exit; 76 } 77} 7879private function checkSignature() 80{ 81 // you must define TOKEN by yourself 82 if (!defined("TOKEN")) { 83 throw new Exception(TOKEN is not defined!); 84 } 8586 $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 ); 9697 if( $tmpStr == $signature ){ 98 return true; 99 }else{100 return false;101 }102}103 }104 105 106 ?>

关键词回复和关注回复

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。