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


Posted in PHP onAugust 08, 2016

本文实例为大家分享了php微信文本消息自动回复 别代码,供大家参考,具体内容如下

1.PHP示例代码下载
 下载地址1:http://xiazai.3water.com/201608/yuanma/phpwx(3water.com).rar
 下载地址2:https://mp.weixin.qq.com/wiki/home/index.html(开始开发-》接入指南-》PHP示例代码下载) 

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

2.wx_sample.php初始代码

<?php
/**
 * wechat php test
 */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->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 = "<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";
  $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();”。

<?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)){
 /* 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 = "<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";
  $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.关注/取消关注事件)

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

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

<?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)){
 /* 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 = "<xml>
  <ToUserName><![CDATA[%s]]></ToUserName>
  <FromUserName><![CDATA[%s]]></FromUserName>
  <CreateTime>%s</CreateTime>
  <MsgType><![CDATA[%s]]></MsgType>
  <Content><![CDATA[%s]]></Content>
  <FuncFlag>0</FuncFlag>
  </xml>"; 
  
 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 相关文章推荐
mysql,mysqli,PDO的各自不同介绍
Sep 19 PHP
php单例模式实现(对象只被创建一次)
Dec 05 PHP
深入php self与$this的详解
Jun 08 PHP
php.ini 配置文件的深入解析
Jun 17 PHP
提高PHP性能的编码技巧以及性能优化详细解析
Aug 24 PHP
CodeIgniter错误mysql_connect(): No such file or directory解决方法
Sep 06 PHP
CI配置多数据库访问的方法
Mar 28 PHP
php处理单文件、多文件上传代码分享
Aug 24 PHP
tp5(thinkPHP5)框架连接数据库的方法示例
Dec 24 PHP
php判断数组是否为空的实例方法
May 10 PHP
实例化php类时传参的方法分析
Jun 05 PHP
PHP日期和时间函数的使用示例详解
Aug 06 PHP
微信公众号开发之语音消息识别php代码
Aug 08 #PHP
PHP+JQuery+Ajax实现分页方法详解
Aug 06 #PHP
微信自定义菜单的创建/查询/取消php示例代码
Aug 05 #PHP
Thinkphp微信公众号支付接口
Aug 04 #PHP
浅析Laravel5中队列的配置及使用
Aug 04 #PHP
PHP中如何判断exec函数执行成功?
Aug 04 #PHP
详解Laravel视图间共享数据与视图Composer
Aug 04 #PHP
You might like
改进的IP计数器
2006/10/09 PHP
PHP HTML代码串截取代码
2008/12/29 PHP
解析PHP的session过期设置
2013/06/29 PHP
PHP中使用gettext解决国际化问题的例子(i18n)
2014/06/13 PHP
win10 apache配置虚拟主机后localhost无法使用的解决方法
2018/01/27 PHP
JavaScript判断是否为数组的3种方法及效率比较
2015/04/01 Javascript
jQuery使用经验小技巧(推荐)
2016/05/31 Javascript
JavaScript实现点击文本自动定位到下拉框选中操作
2016/06/15 Javascript
jQuery解决input元素的blur事件和其他非表单元素的click事件冲突问题
2016/08/15 Javascript
JavaScript使用Range调色及透明度实例
2016/09/25 Javascript
vue实现全选和反选功能
2017/08/31 Javascript
Popup弹出框添加数据实现方法
2017/10/27 Javascript
js闭包学习心得总结
2018/04/17 Javascript
微信小程序下拉刷新PullDownRefresh的使用方法
2018/11/29 Javascript
简单了解JavaScript中常见的反模式
2019/06/21 Javascript
js实现详情页放大镜效果
2020/10/28 Javascript
让Vue响应Map或Set的变化操作
2020/11/11 Javascript
JavaScript 中的六种循环方法
2021/01/06 Javascript
分析Python编程时利用wxPython来支持多线程的方法
2015/04/07 Python
python制作花瓣网美女图片爬虫
2015/10/28 Python
浅谈python中的__init__、__new__和__call__方法
2017/07/18 Python
pandas数据清洗,排序,索引设置,数据选取方法
2018/05/18 Python
Python Tkinter模块实现时钟功能应用示例
2018/07/23 Python
Tensorflow使用支持向量机拟合线性回归
2018/09/07 Python
python获取linux系统信息的三种方法
2020/10/14 Python
python 模拟登录B站的示例代码
2020/12/15 Python
详解Python中的文件操作
2021/01/14 Python
请说出这段代码执行后a和b的值分别是多少
2015/03/28 面试题
函授本科自我鉴定
2013/11/03 职场文书
学生通报表扬范文
2015/05/04 职场文书
全国助残日活动总结
2015/05/11 职场文书
寒假致家长的一封信
2015/10/10 职场文书
学生会2016感恩节活动小结
2016/04/01 职场文书
一篇文章弄懂MySQL查询语句的执行过程
2021/05/07 MySQL
关于CentOS 8 搭建MongoDB4.4分片集群的问题
2021/10/24 MongoDB
手把手带你彻底卸载MySQL数据库
2022/06/14 MySQL