valid();
//开启自动回复功能
$wechatObj->responseMsg();
//定义类文件
class wechatCallbackapiTest
{
//实现valid验证方法:实现对接微信公众平台
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
//接受用户端发送过来的xml数据
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
//判断xml数据是否为空
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);
//通过simplexml进行xml解析
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//接受微信的手机端
$fromUsername = $postObj->FromUserName;
//微信公众平台
$toUsername = $postObj->ToUserName;
//接受用户发送的关键词
$keyword = trim($postObj->Content);
//1.接受用户消息类型
$msgType = $postObj -> MsgType;
//时间戳
$time = time();
//文本发送模板
$textTpl = "%s0";
//////////////////////////////////////////////////////////////////////////////////
//如果用户发送的是文本类型文件,执行以下
if($msgType == 'text'){
if(!empty( $keyword ))
{
/*这是一个实例
//如果发送文本信息
$msgType = "text";
//回复内容
if($keyword == "李楠"){
$contentStr = "叫我干嘛";
}else{
$contentStr = "叫我干嘛";
}
//格式化xml模板,参数与上面的模板是一一对应的.fromUsername和头Username是相反的,只写带%s的
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//将xml信息返回给客户端
echo $resultStr;
*/
if($keyword == "?" || $keyword == "?"){
$msgType = "text";
$contentStr = "1.特种服务号码\n2.通讯服务号码";
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo $resultStr;
}elseif($keyword == 1){
$msgType = "text";
$contentStr = "1.匪警:110\n2.火警:119\n3.急救:120";
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo $resultStr;
}elseif($keyword == 2){
$msgType = "text";
$contentStr = "1.中国移动:10086\n2.中国联通:10010";
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo $resultStr;
}
}else{
echo "不能不说话";
}
}
////////////////////////////////////////////////////////////////////////////////////
//接受图片信息
if($msgType == "image"){
//如果发送文本信息
$msgType = "text";
//回复内容
$contentStr = "你发送的是图片文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//将xml信息返回给客户端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "voice"){
//如果发送文本信息
$msgType = "text";
//回复内容
$contentStr = "你发送的是语音文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//将xml信息返回给客户端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "video"){
//如果发送文本信息
$msgType = "text";
//回复内容
$contentStr = "你发送的是视频文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//将xml信息返回给客户端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "shortvideo"){
//如果发送文本信息
$msgType = "text";
//回复内容
$contentStr = "你发送的是小视频文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//将xml信息返回给客户端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "location"){
//如果发送文本信息
$msgType = "text";
//回复内容
$contentStr = "你发送的是地理位置文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//将xml信息返回给客户端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "link"){
//如果发送文本信息
$msgType = "text";
//回复内容
$contentStr = "你发送的是连接文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//将xml信息返回给客户端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
/*
//判断用户发送关键词是否为空
if(!empty( $keyword ))
{
//如果发送文本信息
$msgType = "text";
//回复内容
$contentStr = "大家好,我是hero";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//将xml信息返回给客户端
echo $resultStr;
}else{
echo "Input something...";
}
*/
}else {
echo "";
exit;
}
}
private function checkSignature()
{
// you must define TOKEN by yourself
//判断是否定义了TOKEN,如果没有就抛出一个异常
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];//接受微信加密签名
$timestamp = $_GET["timestamp"];//接受时间戳
$nonce = $_GET["nonce"];//接受随机数
$token = TOKEN;//把TOKEN常量赋值给$token
//把相关参数组装成数组
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
//排序
sort($tmpArr, SORT_STRING);
//把排序后的数组转换成字符串
$tmpStr = implode( $tmpArr );
//通过哈希算法加密
$tmpStr = sha1( $tmpStr );
//与加密签名进行对比
if( $tmpStr == $signature ){
//相同返回true
return true;
}else{
//不同返回false
return false;
}
}
}
?>
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
本专题围绕Buffalo框架数据库开发,讲解database.yml多环境配置、soda与fizz迁移生成回滚、模型结构体标签、增删改查与条件查询、一对多与多对多关联、数据校验、回调钩子、事务处理及原生SQL执行能力。
0
2026.09.23
本专题讲解Buffalo框架路由与请求处理机制,涵盖路由注册与分组、资源路由、Handler编写规范、Context上下文方法、参数绑定、中间件编写挂载、Session与Cookie读写、Flash消息及错误页面定制方法。
0
2026.09.23
本专题整理Buffalo框架入门内容,涵盖Go环境准备、buffalo CLI安装、新项目生成、目录结构说明、dev热加载启动、数据库连接配置与常见报错排查,帮助新手按约定优于配置的思路跑通第一个Buffalo框架应用。
0
2026.09.23
本专题介绍通过conanfile.py创建软件包的方法,讲解包名、版本、依赖和构建设置等基础信息,以及source、build、package、package_info等常用方法的作用及编写思路。
0
2026.09.22
本专题介绍Conan根据操作系统、编译器、架构和构建类型生成二进制包的方法,讲解Profile、Settings、Options及Package ID的作用,帮助管理不同平台和编译环境下的包版本。
0
2026.09.22
本专题系统的讲解Conan私有仓库的搭建流程,涵盖仓库服务部署、存储目录配置、用户认证、权限划分和远程地址添加,并介绍内部C++依赖包的上传、下载及版本维护方法。
0
2026.09.22
本专题汇总了 Loomy 桌面 AI 助理的官方入口地址合集及使用指南。提供 macOS 与 Windows 客户端下载 。Loomy 是讯飞推出的桌面级 AI 工作搭子,支持文件整理、数据分析、网页操作及通过飞书/钉钉远程操控电脑,助你高效完成本地办公任务 。
0
2026.09.22
本专题整理 NumPy 常见函数使用方法相关教程,覆盖函数大全、参数用法、数组运算、统计聚合、排序处理、where 条件筛选、linspace 创建数列等常用场景,帮助读者快速掌握 NumPy 函数调用思路和实际数据处理技巧。
0
2026.09.22
本专题整理 NumPy 性能优化、版本更新与常见报错排查相关教程,覆盖向量化计算、广播性能、内存布局、NumPy 2.0 升级、版本兼容冲突、安装导入报错、dtype 溢出、矩阵运算异常和 broadcasting 报错修复,帮助读者系统掌握 NumPy 性能调优与问题定位方法。
20
2026.09.22
热门下载
精品课程
共67课时 | 12.1万人学习









