php微信接口实例
<?php
/**
* wechat php test
*/ //define your token
//定义TOKEN秘钥
define("TOKEN", "weixin"); //实例化微信对象
$wechatObj = new wechatCallbackapiTest();
//验证成功后注释valid方法
//$wechatObj->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 = "<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($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;
}
}
} ?>
php微信接口实例的更多相关文章
- C#开发微信门户及应用(42)--使用Autofac实现微信接口处理的控制反转处理
在很多情况下,我们利用IOC控制反转可以很方便实现一些接口的适配处理,可以在需要的时候切换不同的接口实现,使用这种方式在调用的时候,只需要知道相应的接口接口,具体调用哪个实现类,可以在配置文件中动态指 ...
- C#.NET微信公众账号接口开发系列文章整理--微信接口开发目录,方便需要的博友查询
前言: 涉及微信接口开发比较早也做的挺多的,有时间的时候整理了开发过程中一些思路案例,供刚学习微信开发的朋友参考.其实微信接口开发还是比较简单的,但是由于调试比较麻烦,加上微信偶尔也会给开发者挖坑,并 ...
- Django:之中间件、微信接口和单元测试
Django中间件 我们从浏览器发出一个请求 Request,得到一个响应后的内容 HttpResponse ,这个请求传递到 Django的过程如下: 也就是说,每一个请求都是先通过中间件中的 pr ...
- python实现微信接口(itchat)
python实现微信接口(itchat) 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实 ...
- RestTemplate 微信接口 text/plain HttpMessageConverter
一.背景介绍 使用 Spring Boot 写项目,需要用到微信接口获取用户信息. 在 Jessey 和 Spring RestTemplate 两个 Rest 客户端中,想到尽量不引入更多的东西,然 ...
- python实现微信接口——itchat模块
python实现微信接口——itchat模块 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方 ...
- C#微信接口之推送模板消息功能示例
本文实例讲述了C#微信接口之推送模板消息功能.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...
- C#开发微信门户及应用(1)--开始使用微信接口
微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习下微信的相关开发,也就成为日常计划的重要事情之一了.本系列文章希望从一个循序渐进的角度上,全面介 ...
- 练习题(登陆-进度条-微信接口判断qq-微信接口判断列车时刻表-)
1.写一个用户的登陆注册的界面,用户的密码用hashlib加密存在文件中,登陆时候,用户的密码要和文件中的密码一致才行 def sha(password): #加密函数 passwd = hashli ...
随机推荐
- WinForm程序打包说明
如果使用的是VS2013需要下载并安装 Microsoft Visual Studio 2013 Installer Projects 下载地址:https://visualstudiogallery ...
- shell脚本参数
举例说:脚本名称叫test.sh 入参三个: 1 2 3运行test.sh 1 2 3后$*为"1 2 3"(一起被引号包住)$@为"1" "2&qu ...
- Linux (Ubuntu) 下配置VPN服务器
昨天网上找了下VPN的相关信息,居然各种撞墙,特别郁闷,自己不容易找到的东西,记录下VPN的配置信息 ubuntu 13.1下配置VPN ,采用PPTP实现, 第一步.安装pptpd,没有安装包记得 ...
- sharepoint Report使用共享数据源部署报错
在VS中做了一下报表,把数据源设置成为共享数据源,部署时发生以下错误: 解决方法:在VS中解决方案窗口,选择共享数据源中的用到的数据源名称,右键点击,选择“部署”,部署成功后,再部署报表即可.
- iOS开发UI篇—Button基础
iOS开发UI篇—Button基础 一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状 ...
- Regsvr32.exe 用法
RegSvr32.exe 具有以下命令行选项: Regsvr32 [/u] [/n] [/i[:cmdline]] dllname /u - 取消注册服务器 /i - 调用 DllInstall,为 ...
- 【转】Ant学习笔记——自己构建Ant编译环境
自从年初开始用NetBeans6.0,才接触到Ant. 这是今年6月份的一篇Ant学习笔记.安装 1.下载并构建环境. 去官网下载src包和bin包.解压缩它们到同一目录,运行build.bat, ...
- Spring 中的 JDBC 事务
Spring 对 JDBC 的支持 JdbcTemplate 简介 •为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架. • ...
- 搭建web服务器环境
一. 安装apache 安装好之后测试:浏览器地址栏输入:localhost,若弹出"It works!"表明已成功安装. 管理方式:1.通过Apache自带的镜像管理器:2.wi ...
- OC中intValue要注意的地方
在程序中,发现一个问题,写了个例子,如下: NSDictionary * dict = [[NSDictionary alloc] init]; NSString * s ...