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 ...
随机推荐
- WIN7 java7 环境变量配置
1 首先要搞清楚,java可能安装了两个部分,一个叫JDK,一个叫jre.其中jre若已经安装了,那么在安装JDK的时候,就不要放在默认的C:\Program File\java\XXX目录.最好另起 ...
- NGINX 定时器
写在前面 写NGINX系列的随笔,一来总结学到的东西,二来记录下疑惑的地方,在接下来的学习过程中去解决疑惑. 也希望同样对NGINX感兴趣的朋友能够解答我的疑惑,或者共同探讨研究. 整个NGINX系列 ...
- css实现一段不够一行时居中显示,多于一行时两端对齐
今天有遇到这个问题,不够一行要居中才好看,多于一行居中又很难看: 居中 两端对齐 一开始想用text-align-last:center; 可是这样结果是这样的: 单行的居中了 可是多行的最后一行也居 ...
- warning MSB3391
1.用C#生成dll文件 提示 (warning MSB3391)"未包含任何可为 COM 互操作注销的类型"和 ( warning MSB3214)"不包含任何可为 C ...
- 2016.9.18 --- Shenyang ol
1001 Resident Evil 1002 List wants to travel 1003 hannnnah_j’s Biological Test 1004 Mathematician QS ...
- 超实用的JavaScript技巧及最佳实践
众所周知,JavaScript是一门非常流行的编程语言,开发者用它不仅可以开发出炫丽的Web程序,还可以用它来开发一些移动应用程序(如PhoneGap或Appcelerator),它还有一些服务端实现 ...
- iOS开发UI篇—Kvc简单介绍
ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...
- 跨域资源共享 CORS
CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...
- jQueryMobile控件之ListView
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- poj2253
此题略坑,%.3lf用g++一直WA,c++过的 //Accepted 468 KB 16 ms #include <cstdio> #include <cstring> #i ...