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 ...
随机推荐
- Bitmap旋转方法
最近在做一个ORC图片解析的功能,要求解析出数字 但是发现他解析只能解析横着的图片,然后我拍照的时候拍的是竖直照片,当然你也可以去旋转照相机的屏幕 但是我这里为了方便选择的是竖直拍出来 然后,旋转下咯 ...
- Caché数据库学习笔记(2)
目录: 创建新类(表)(class文件)与创建routine(.mac .inc) 在类里面添加函数(classmethod) Terminal的使用 ======================= ...
- 基于zepto的一个日期区间选择插件
功能:类似去哪选日期的那个日历,选第二个日期关闭日历,https://github.com/zhangchen2397/calendar在这个插件基础上改哒,这个只能选一个日期,我加了选两个日期的.并 ...
- 转-Asp.Net MVC及Web API框架配置会碰到的几个问题及解决方案
前言 刚开始创建MVC与Web API的混合项目时,碰到好多问题,今天拿出来跟大家一起分享下.有朋友私信我问项目的分层及文件夹结构在我的第一篇博客中没说清楚,那么接下来我就准备从这些文件怎么分文件夹说 ...
- spring mvc 注解 学习笔记(一)
以前接触过spring,但是没有接触spring mvc 以及注解的应用,特习之,记之: 注解了解 @Component 是通用标注, @Controller 标注web控制器, @Service 标 ...
- iOS开发Swift篇—(十)方法
iOS开发Swift篇—(十)方法 一.简单说明 跟其他面向对象语言一样,Swift中的方法可以分为2大类: (1)实例方法(Instance Methods) 在OC中,实例方法以减号(-)开头 ( ...
- SQL Server 中的 NOLOCK 用法
大家都知道,每新建一个查询,都相当于创建一个会话,在不同的查询分析器里面进行的操作,可以影响到其他会话的查询,极端的情况可能会一直处于阻塞中,哪怕只是一个很简单的查询都“特别慢”. BEGIN TRA ...
- Linux在目录中查找某个函数
1,在某个路径下查文件. 在/etc下查找“*.log”的文件 find /etc -name “*.log” 2,扩展,列出某个路径下所有文件,包括子目录. find /etc -name “*” ...
- 【转】解决编译安装NGINX时make报错
编译参数:--[root@localhostnginx-1.4.6]#./configure--user=nginx--group=nginx--prefix=/usr/local/nginx--wi ...
- bcopy函数
函数原型:void bcopy(const void *src, void *dest, int n) 头文件:#include <string.h> 函数功能:将src指针指 ...