微信公众号开发之文本消息自动回复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 相关文章推荐
解析strtr函数的效率问题
Jun 26 PHP
destoon调用discuz论坛中带图片帖子的实现方法
Aug 21 PHP
php字符串按照单词进行反转的方法
Mar 14 PHP
如何使用微信公众平台开发模式实现多客服
Jan 06 PHP
PHP+Ajax异步带进度条上传文件实例
Nov 01 PHP
php 二维数组时间排序实现代码
Nov 19 PHP
完美解决thinkphp唯一索引重复时出错的问题
Mar 31 PHP
stripos函数知识点实例分享
Feb 11 PHP
详解PHP PDO简单教程
May 28 PHP
PHP查找一列有序数组是否包含某值的方法
Feb 07 PHP
php 下 html5 XHR2 + FormData + File API 上传文件操作实例分析
Feb 28 PHP
yii框架结合charjs统计上一年与当前年数据的方法示例
Apr 04 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
聊天室php&amp;mysql(五)
2006/10/09 PHP
探讨PHP调用时间格式的参数详解
2013/06/06 PHP
php mail to 配置详解
2014/01/16 PHP
Extjs列表详细信息窗口新建后自动加载解决方法
2010/04/02 Javascript
JavaScript的类型转换(字符转数字 数字转字符)
2010/08/30 Javascript
如何从jQuery的ajax请求中删除X-Requested-With
2013/12/11 Javascript
Select标签下拉列表二级联动级联实例代码
2014/02/07 Javascript
js获取元素外链样式的方法
2015/01/27 Javascript
javascript实现checkbox全选的代码
2015/04/30 Javascript
avalonjs实现仿微博的图片拖动特效
2015/05/06 Javascript
JS实现简单的图书馆享元模式实例
2015/06/30 Javascript
JavaScript动态插入CSS的方法
2015/12/10 Javascript
json对象转为字符串,当做参数传递时加密解密的实现方法
2016/06/29 Javascript
javascript中Number的方法小结
2016/11/21 Javascript
layui.js实现的表单验证功能示例
2017/11/15 Javascript
解决Vue2.0 watch对象属性变化监听不到的问题
2018/09/11 Javascript
vue中进入详情页记住滚动位置的方法(keep-alive)
2018/09/21 Javascript
javascript刷新父页面方法汇总详解
2019/10/10 Javascript
[01:02:18]VGJ.S vs infamous Supermajor 败者组 BO3 第一场 6.4
2018/06/05 DOTA
Python使用迭代器捕获Generator返回值的方法
2017/04/05 Python
Python3中在Anaconda环境下安装basemap包
2018/10/21 Python
pygame游戏之旅 添加游戏介绍
2018/11/20 Python
利用python脚本如何简化jar操作命令
2019/02/24 Python
Pytorch实现GoogLeNet的方法
2019/08/18 Python
基于python实现ROC曲线绘制广场解析
2020/06/28 Python
Python grequests模块使用场景及代码实例
2020/08/10 Python
Python实现微信表情包炸群功能
2021/01/28 Python
HTML5 文件上传下载的实例代码
2017/07/03 HTML / CSS
美国中小型企业领先的办公家具供应商:Office Designs
2016/11/26 全球购物
党校培训自我鉴定范文
2014/04/10 职场文书
教师个人自我评价范文
2014/04/13 职场文书
党员群众路线承诺书
2014/05/20 职场文书
PHP新手指南
2021/04/01 PHP
使用Oracle跟踪文件的问题详解
2021/06/28 Oracle
IIS服务器中设置HTTP重定向访问HTTPS
2022/04/29 Servers
MyBatis核心源码深度剖析SQL语句执行过程
2022/05/20 Java/Android