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

1.PHP示例代码下载 下载地址1:http://xiazai.jb51.net/201608/yuanma/phpwx(jb51.net).rar

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

2.wx_sample.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 = "

%s

0

";

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();”。

/**

* 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 = "

%s

0

";

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。

/**

* 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 = "

%s

0

";

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代码相关推荐

  1. 微信语音识别 php,微信公众号开发之语音消息识别php代码

    本文实例为大家分享了php微信语音消息识别代码,供大家参考,具体内容如下 1.开通语音识别(默认关闭) 2.语音识别 请注意,开通语音识别后,用户每次发送语音给公众号时,微信会在推送的语音消息XML数 ...

  2. 微信公众号php编程,PHP编程:微信公众号开发之文本消息自动回复php代码

    搜索热词 <微信公众号开发之文本消息自动回复PHP代码>要点: 本文介绍了微信公众号开发之文本消息自动回复PHP代码,希望对您有用.如果有疑问,可以联系我们. 本文实例为大家分享了PHP微 ...

  3. 微信公众 php代码,微信公众号开发之文本消息自动回复php代码_php实例

    本文实例为大家分享了php微信文本消息自动回复 别代码,供大家参考,具体内容如下 1.PHP示例代码下载 下载地址1:http://xiazai.php.net/201608/yuanma/phpwx ...

  4. java 微信自定义菜单开发_微信公众号开发之设置自定义菜单实例代码【java版】...

    本实例是为了实现在管理后台实现微信菜单的添加删除管理. 1.首先我们需要新建一个数据库表用于存放menu菜单项 可包含的字段有id.父类id.name.排序.是否显示.类型(view.click).链 ...

  5. 微信公众号开发接收图片消息,获取用户发送图片的media_id

    现在的手机都有拍照功能, 而且人们拍完照后, 喜欢跟朋友分享. 微信公众号也提供了一个功能, 允许你的粉丝在后台回复图片, 你可以通过公众平台提供的api接口, 获取到图片media_id, 然后把图 ...

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

    本文实例为大家分享了php微信文本消息自动回复 别代码,供大家参考,具体内容如下 1.PHP示例代码下载 下载地址1:http://xiazai.zyiz.net/201608/yuanma/phpw ...

  7. 微信公众号php二次开发,微信公众号开发之文本消息自动回复php代码

    本文实例为大家分享了php微信文本消息自动回复 别代码,供大家参考,具体内容如下 1.PHP示例代码下载 下载地址1:http://xiazai.jb51.net/201608/yuanma/phpw ...

  8. 公众平台自动回复php代码,微信公众号开发之文本消息自动回复php代码

    本文实例为大家分享了php微信文本消息自动回复 别代码,供大家参考,具体内容如下 1.PHP示例代码下载 下载地址1:http://xiazai.gimoo.net/201608/yuanma/php ...

  9. 微信公众号开发之自动消息回复

    微信公众号后台有编辑模式和代码模式两种,设置成代码模式后,微信服务器会把公众号收到的所有消息以post方式发送到验证接入填写的url上,携带的请求参数为xml格式,由自己写的controller负责处 ...

最新文章

  1. nyoj-66 分数拆分
  2. Django中STATIC_URL、STATIC_ROOT、STATICFILES_DIRS 的区别关系
  3. android trace文件分析ANR
  4. C语言十进制数转换为八进制(附完整源码)
  5. Django从理论到实战(part49)--ModelForm
  6. how is our custom css style inserted to html
  7. 策略模式与简单工厂模式区别(转)
  8. 分享一套非常不错的bootstarp3.0.2响应式模板
  9. 狗狗币暴涨暴跌?数据分析师带你走进它的前世今生!
  10. Java游戏用户登录注册_Java实现多用户注册登录的幸运抽奖
  11. Redis(八):Redis的复制(Master/Slave)
  12. 我的世界pe开服务器网站,我的世界pe服务器怎么开 开服图文教程
  13. shiro实战系列(十一)之Caching
  14. 编辑器 UEditor 百度富文本web编辑器
  15. java.util.concurrent.ThreadFactory 实例讲解
  16. 青少年编程Playgrounds之十八--算法
  17. 黑苹果 惠普笔记本电池补丁_笔记本的黑苹果之路
  18. 白骨精写给孙悟空的信
  19. 汉澳sinox载入ntfs硬盘,移动ntfs硬盘,u盘 并读写完全成功
  20. 为什么说BMC才是国产服务器的“命门”?

热门文章

  1. 博图14软件安装顺序_博图软件安装顺序 为什么博途用的人不多
  2. 小学奥数思维训练题(十三)
  3. 单片机按键设计的四个方案
  4. Spring学习笔记5
  5. TSMaster快速入门篇(2)-报文回放
  6. DATAKIT CrossManager 2022.4 Crack
  7. 3. java缓存-线程内缓存guava cache
  8. python使用Neo4j图数据库——py2neo详解(1)
  9. java Eclipes配置黑色框架
  10. WPF开发必备--类库