微信接口类

酷磊酱_3091

酷磊酱_3091

2016-08-22

1702人浏览

原创


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;
		}
	}
}

?>


微信app下载
微信app下载

微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Vite 项目如何打包和部署
Vite 项目如何打包和部署

Vite项目打包部署专题汇集生产构建、产物预览、输出目录、资源路径、Nginx与GitHub Pages部署及常见白屏和路由404问题的已审核文章,帮助读者完成上线流程。

2026.10.28

80

16

Vite 如何搭配 Vue.js
Vite 如何搭配 Vue.js

Vite搭配Vue.js专题汇集创建Vue 3项目、配置开发环境、路径别名、Vue插件、代理、多环境构建与调试的已审核文章,帮助读者完成从初始化到日常开发的配置。

2026.10.28

60

15

Vite 项目目录怎么看
Vite 项目目录怎么看

Vite项目目录专题汇集入口文件、src源码、public静态资源、路径别名、项目根目录和构建输出目录相关的已审核文章,帮助读者理解各文件夹的用途与引用方式。

2026.10.28

200

10

Gradle插件开发入门教程
Gradle插件开发入门教程

本专题介绍Gradle插件开发的基本流程,涵盖插件类编写、自定义任务注册、扩展参数设计和插件应用方法,帮助你理解插件如何组织构建逻辑,减少多个项目之间的重复配置。

2026.10.22

140

11

Gradle Wrapper配置教程
Gradle Wrapper配置教程

本专题介绍Gradle Wrapper的基本作用,涵盖文件组成、生成方法、版本配置和任务调用,帮助你理解项目如何使用指定的Gradle版本,减少不同开发环境带来的构建差异。

2026.10.22

120

10

Golangci-lint检查器使用教程
Golangci-lint检查器使用教程

本专题整理Golangci-lint常用Linters的用途与配置方法,覆盖错误处理、无效代码、安全、复杂度、命名和依赖检查,帮助项目合理选规则。

2026.10.22

80

15

Gradle怎么构建Java项目
Gradle怎么构建Java项目

本专题介绍Gradle Java项目构建方法,涵盖Java插件配置、依赖声明、源码编译、测试执行和打包检查,帮助你理解各阶段的任务关系,逐步完成项目构建。

2026.10.22

60

10

PHP时间戳漏洞修复教程
PHP时间戳漏洞修复教程

本专题汇总PHP常见时间戳漏洞,包括2038年溢出、时区漂移、时间戳伪造、重放攻击、时间盲注与日志审计失效等,并给出升级64位环境、使用 DateTimeImmutable、统一UTC、严格超时校验、预处理绑定、hash_equals 比对等修复方案,帮助开发者快速排查和加固时间相关代码。

2026.10.22

80

25

Golangci-lint配置文件与规则设置
Golangci-lint配置文件与规则设置

本专题聚焦Golangci-lint YAML配置文件,讲清运行参数、规则启停、目录排除、问题过滤及严重级别设置,便于团队统一检查标准。

2026.10.22

40

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.7万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 134.1万人学习