1.微信公众平台开发
1.1开发者模式
1).微信公众平台账号(http://mp.weixin.qq.com)
订阅号:个人版用户,每天可群发一条消息
服务号:企业版用户,每月可以发送四条消息
2).在线虚拟主机或者服务器,(SAM云引擎,BAE云引擎,阿里云服务器)
3).TortoiseSVN(SVN客户端软件),把代码放到服务器上
1.2开发原理图
解释:手机端用户发送消息到腾讯服务器,腾讯服务器的内部源代码解析后发送给我们自定义的放服务器,其中TortoiseSVN主要是把我们自己的源代码放到自定义服务其中,自定义服务器通过内部源代码处理腾讯服务器发过来的xml,再返回给腾讯服务器,腾讯服务器经过 处理再返回给用户,实现完整流程。
2.申请测试号,配置接口信息
代码如下
<?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()
{
//通过get请求接收随机字符串
$echoStr = $_GET["echostr"];
//valid signature , option
//进行用户数字签名验证
if($this->checkSignature()){
//如果成功,则返回接收到的随机字符串
echo $echoStr;
//退出
exit;
}else{
echo 'cuowu';
}
}
//定义自动回复功能
public function responseMsg()
{
//get post data, May be due to the different environments
//接收用户端发送过来的xml数据
// $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$postStr = file_get_contents("php://input");
//extract post data
//判断xml数据是否为空
if (!empty($postStr)){
//通过simplexml进行xml解析
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//手机端
$fromUsername = $postObj->FromUserName;
//微信公众平台
$toUsername = $postObj->ToUserName;
echo $toUsername;
//接收用户发送的关键词
$keyword = trim($postObj->Content);
//接收用户消息类型
$msgType = $postObj->MsgType;
echo $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 ))
{
//回复类型,如果为"text",代表文本类型
$msgType = "text";
//回复内容
$contentStr = "你发送的是文本消息";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//把xml数据返回给手机端
echo $resultStr;
}else{
echo "Input something...";
}
}elseif($msgType=="image"){
//回复类型,如果为"text",代表文本类型
$msgType = "text";
//回复内容
$contentStr = "你发送的是图片消息";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//把xml数据返回给手机端
echo $resultStr;
}elseif($msgType=="voice"){
//回复类型,如果为"text",代表文本类型
$msgType = "text";
//回复内容
$contentStr = "你发送的是语音消息";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//把xml数据返回给手机端
echo $resultStr;
}elseif($msgType=="shortvideo"){
//回复类型,如果为"text",代表文本类型
$msgType = "text";
//回复内容
$contentStr = "你发送的是短视频消息";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//把xml数据返回给手机端
echo $resultStr;
}
elseif($msgType=="video"){
//回复类型,如果为"text",代表文本类型
$msgType = "text";
//回复内容
$contentStr = "你发送的是视频消息";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//把xml数据返回给手机端
echo $resultStr;
}
}else {
echo "";
exit;
}
}
//定义checkSignature
private function checkSignature()
{
//判断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);
//通过字典法进行排序
sort($tmpArr);
//把排序后的数组转化成字符串
$tmpStr = implode( $tmpArr );
//通过哈希算法对字符串进行加密
$tmpStr = sha1( $tmpStr );
//与加密签名进行对比
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
3.回复功能测试
就是上述代码,$GLOBALS好多没有打开,建议用下图