welcomeMsg="HELLO,欢迎订阅!";
//$this->main(); //*****************第一次验证的时候打开,随后可关闭***************
}
//首次TOKEN验证
public function main(){
$echoStr=$_GET['echostr'];
if($this->checkRes()){
echo $echoStr;
}else{
exit();
}
}
//验证权限
private function checkRes(){
$signature=$_GET['signature']; //获取微信服务发送过来的参数
$timestamp=$_GET['timestamp'];
$nonce=$_GET['nonce'];
$token = TOKEN;
$tmpArr = array($token,$timestamp,$nonce);
sort($tmpArr); //进行字典排序
$strWaitCheck = implode($tmpArr); //进行字符串组合
$strWaitCheck = sha1($strWaitCheck); //对字符串进行sha1加密
if($strWaitCheck==$signature){
return true;
}else{
return false;
}
}
public function getWhatWeChatServerPost(){
$huntPostContent = $GLOBALS["HTTP_RAW_POST_DATA"]; //获取微信服务post过来的内容
$newXmlObj =simplexml_load_string($huntPostContent,'SimpleXMLElement', LIBXML_NOCDATA); //把post过的字符串载入对象
$this->fromUserName = $newXmlObj->FromUserName; //向公共账号发信息的用户名
$this->toUserName = $newXmlObj->ToUserName; //公共账号名
$this->keyWords = trim($newXmlObj->Content); //消息内容
$this->msgId = trim($newXmlObj->MsgId); //读取当前消息msgId
$this->msgType = trim($newXmlObj->MsgType); //读取当前消息类型
$this->event = trim($newXmlObj->Event); //读取当前事件名
$this->format = trim($newXmlObj->Format); //语音格式:amr
$this->mediaId = trim($newXmlObj->MediaID); //语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体
$this->recognition = trim($newXmlObj->Recognition); //语音识别结果,UTF8编码
}
//消息响应
public function responseMsg(){
$this->getWhatWeChatServerPost(); //执行接收微信服务器POST过来的东西
//当消息类型为text的时候处理
if(!empty($this->keyWords)&& $this->msgType == "text"){
$para = $this->keyWords;
switch($para){
case "1":
$tmp_item = $this->textPicMaxTmp(
"this is a test!!",
"各位大叔大妈别太大较真,这就一测试滴",
"http://www.baidu.com/img/bdlogo.gif",
"http://www.baidu.com/");
$re_global = $this->resTextPicMax($this->fromUserName,$this->toUserName,1,$tmp_item);
echo $re_global;
break;
case "2":
$contentResText = "Hello,buddy,you send me a \"2\"[玫瑰]";
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
echo $re_global;
break;
case "000":
$contentResText = "你想返回的字符串";
$this->longStrHandleExtendfunc($contentResText);
break;
default:
//调用功能判断函数处理
$this->longStrHandle($para);
break;
}
}else if($this->msgType == "event" && !empty($this->event)){ //获取事件类型
if($this->event=='subscribe'){
$contentResText = $this->welcomeMsg; //初次关注的时候推送欢迎消息
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
}else if($this->event == "unsubscribe"){///这个地方好像没有作用!!!
$contentResText = "我擦,你咋跑了";
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
}
}else if($this->msgType == "voice"){
if($this->recognition == ""){
$contentResText ="亲,你说的火星文么,听不懂,建议您还是文字查询吧..[呲牙]";//语音无返回结果的时候给用记返回一条错误
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
}else{
$contentResText =$this->recognition; //其它情况下返回欢迎语
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
}
}else{
//如果在text情况意外得到一条空消息的处理
echo "";
exit();
}
echo $re_global; //返回值给微信服务器
}
//多次用到的几句话就定义了一个小方法
public function longStrHandleExtendfunc($conText){
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$conText);
echo $re_global;
}
//收到除特定字符外的其它字符的处理方法,这个要根据你需要的具体逻辑处理
public function longStrHandle($waitHandleStr){
/*
your code
*/
}
//回复文本信息方法
public function resTextMsg($tu,$fu,$textContent){
//文本消息模板
$textTpl = "
%s
0
";
$msgType = "text"; //消息类型
$time = time(); //时间戳
$resServerStr = sprintf($textTpl,$tu,$fu,$time,$msgType,$textContent); //替换模板里的内容
return $resServerStr;
}
//回复图文
public function resTextPicMax($tu,$fu,$itemNum,$totalItemStr){
//图片模板
$picTpl = "
%s
%s
"
.$totalItemStr.
"
";
$msgType = "news"; //消息类型
$time = time(); //时间戳
$resServerStr = sprintf($picTpl,$this->fromUserName,$this->toUserName,$time,$msgType,$itemNum); //替换模板里的内容
return $resServerStr;
}
//图文模板处理
public function textPicMaxTmp($title,$des,$picUrl,$jumpUrl){
//图文消息条目模板
$itemTpl="
-
";
$resItemStr = sprintf($itemTpl,$title,$des,$picUrl,$jumpUrl); //替换模板里的内容
return $resItemStr;
}
//接下来就是你自己定义的各种方法
} 0
0
相关文章
VSC怎样在VSC中管理PHP会话_会话控制设置步骤【说明】
宝塔面板php怎么下载安装_可视化添加PHP版本教程【教程】
php后缀怎么变mp4打不开_修改后文件无法打开解决办法【技巧】
旧版短链接怎么还原php_适配老版本加密规则的解码法【详解】
php做exe新手怎么测试exe_运行检测及问题排查技巧【指南】
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!
7
2025.12.31
想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。
4
2025.12.31
想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!
2
2025.12.31
热门下载
精品课程
最新文章







