<?php
define ( "TOKEN", "tony" );
if (! isset ( $_GET ["echostr"] )) {
// 调用响应消息函数
responseMsg ();
} else {
// 实现网址接入,调用验证消息函数
valid ();
}
/*
* 验证消息函数
*/
function valid() {
if (checkSignature ()) {
$echoStr = $_GET ["echostr"];
echo $echoStr;
exit ();
} else {
echo "error";
exit ();
}
}
/*
* 检查签名函数
*/
function checkSignature() {
// you must define TOKEN by yourself
if (! defined ( "TOKEN" )) {
throw new Exception ( 'TOKEN is not defined!' );
}
$signature = $_GET ["signature"];
$timestamp = $_GET ["timestamp"];
$nonce = $_GET ["nonce"];
$token = TOKEN;
$tmpArr = array (
$token,
$timestamp,
$nonce
);
// use SORT_STRING rule
sort ( $tmpArr, SORT_STRING );
$tmpStr = implode ( $tmpArr );
$tmpStr = sha1 ( $tmpStr );
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
/*
* 响应消息函数
*/
function responseMsg() {
// 根据用户传过来的消息类型进行不同的响应
// 接受微信服务器POST过来的XML数据包
$postData = $GLOBALS ["HTTP_RAW_POST_DATA"];
if (empty ( $postData )) {
echo "error";
exit ();
}
// 解析XML数据包
libxml_disable_entity_loader ( true ); // 禁用加载外部实体的能力
$object = simplexml_load_string ( $postData, 'SimpleXMLElement', LIBXML_NOCDATA ); // 解释XML字符串为一个对象
// 获取消息类型
$MsgType = $object->MsgType;
switch ($MsgType) {
case 'event' :
receiveEvent ( $object );
break;
// 接收文本消息
case 'text' :
echo receiveText ( $object );
break;
// 接收图片消息
case 'image' :
echo receiveImage ( $object );
break;
// 接收地理位置消息
case 'location' :
echo receiveLocation ( $object );
break;
// 接收语音消息
case 'voice' :
echo receiveVoice ( $object );
break;
// 接收视频消息
case 'video' :
echo receiveVideo ( $object );
break;
// 接收链接消息
case 'link' :
echo receiveLink ( $object );
break;
default :
echo receiveText ( "无法识别该消息类型" );
break;
}
}
/*
* 接收文本消息函数
*/
function receiveText($obj) {
// 获取文本内容
$Content = "您刚刚发送的消息为:" . $obj->Content;
// 发送文本消息
return replyText ( $obj, $Content );
}
/*
* 接受图片消息函数
*/
function receiveImage($obj) {
// 获取图片消息的内容
$imageArr = array (
"PicUrl" => $obj->PicUrl,
"MediaId" => $obj->MediaId
);
// 发送图片消息
return replyImage ( $obj, $imageArr );
}
/*
* 接受地理位置消息函数
*/
function receiveLocation($obj) {
// 获取地理位置消息的内容
$locationArr = array (
'Location_X' => '地理位置经度:' . $obj->Location_X,
'Location_Y' => '地理位置经度:' . $obj->Location_Y,
'Label' => $obj->Label
);
// 回复文本消息
return replyText ( $obj, $locationArr ['Location_Y'] );
}
/*
* 接收语音消息
*/
function receiveVoice($obj) {
// 获取语音消息
$voiceArr = array (
"MediaId" => $obj->MediaId,
"Format" => $obj->Format,
"MsgID" => $obj->MsgID
);
// 回复语音消息
return replyVoice ( $obj, $voiceArr ["MediaId"] );
}
/*
* 接收事件消息
*/
function receiveEvent($obj) {
$Event = $obj->Event;
switch ($Event) {
// 关注事件
case 'subscribe' :
// 如果$object->EventKey不为空则为:
// 扫描带参数二维码事件,用户未关注时,进行关注后的事件推送
if (! empty ( $obj->EventKey )) {
echo replyText ( $obj, "欢迎扫描关注Tony的微信公众平台" );
} else {
echo replyText ( $obj, "欢迎关注Tony的微信公众平台" );
}
break;
// 取消关注事件
case 'unsubscribe' :
// code...
break;
// 扫描带参数二维码事件,用户已关注时的事件推送
case 'SCAN' :
// code...
break;
// 自定义菜单事件
case 'CLICK' :
$EventKey = $obj->EventKey; // 事件KEY值,与自定义菜单接口中KEY值对应
switch ($EventKey) {
case 'tuwen' :
$newsArr = array (
array (
"Title" => "这是图文信息的标题1",
"Description" => "描述1",
"PicUrl" => "http://mmbiz.qpic.cn/mmbiz/C9jRZGqdOsM5v8w58K9DTwVn6RpwnLCsHLjnNXUib8nD66rGQaL1iaFI9q1qfXLfk1IJO2GicCibPFhWyTlcIADNpA/0",
"Url" => "http://mp.weixin.qq.com/s?__biz=MzA5NzY1NjgwOQ==&mid=200193946&idx=1&sn=4aff70dbb45d7f8e9f3904702bfad3d2#rd"
),
array (
"Title" => "这是图文信息的标题2",
"Description" => "描述2",
"PicUrl" => "http://mmbiz.qpic.cn/mmbiz/C9jRZGqdOsM37wGEwxKxmC4v10vhJsHTCSjyLmEGBcmVqsLhK5f9Yp7syaL2XnGYdISF8KDLvJcDBuG7Ip3nbg/0",
"Url" => "http://mp.weixin.qq.com/s?__biz=MzA5NzY1NjgwOQ==&mid=200191222&idx=1&sn=615458ea5dbaa91b2f4a16ef73ad639b#rd"
)
);
echo replyNews ( $obj, $newsArr );
break;
case 'V1001_GOOD' :
echo replyText ( $obj, "感谢您的赞扬" );
break;
default :
echo replyText ( $obj, "你点击的是其他的事件" );
break;
}
break;
}
}
/*
* 接收视频消息
*/
function receiveVideo($obj) {
$videoArr = array (
"MediaId" => $obj->MediaId,
"ThumbMediaId" => $obj->ThumbMediaId
);
// 回复视频消息
replyVideo ( $obj, $videoArr ["MediaId"] );
}
/*
* 接收链接消息
*/
function receiveLink($obj) {
$linkArr = array (
"Title" => $obj->Title,
"Description" => $obj->Description,
"Url" => $obj->Url,
"MsgId" => $obj->MsgId
);
// 回复文本消息
return replyText ( $obj, $linkArr ['Url'] );
}
/*
* 发送文本消息函数
*/
function replyText($obj, $Content) {
$replyXML = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$FromUserName = $obj->FromUserName;
$ToUserName = $obj->ToUserName;
$time = time ();
// 返回XML数据包
$resultStr = sprintf ( $replyXML, $FromUserName, $ToUserName, $time, $Content );
return $resultStr;
}
/*
* 发送图片消息函数
*/
function replyImage($obj, $imageArr) {
$replyXML = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
$FromUserName = $obj->FromUserName;
$ToUserName = $obj->ToUserName;
$time = time ();
// 返回XML数据包
$resultStr = sprintf ( $replyXML, $FromUserName, $ToUserName, $time, $imageArr ["MediaId"] );
return $resultStr;
}
/*
* 回复语音消息
*/
function replyVoice($obj, $voiceArr) {
$replyXML = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>
</xml>";
$FromUserName = $obj->FromUserName;
$ToUserName = $obj->ToUserName;
$time = time ();
// 返回XML数据包
$resultStr = sprintf ( $replyXML, $FromUserName, $ToUserName, $time, $voiceArr );
return $resultStr;
}
/*
* 回复视频消息
*/
function replyVideo($obj, $videoArr) {
$replyXML = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<Video>
<MediaId><![CDATA[%s]]></MediaId>
</Video>
</xml>";
$FromUserName = $obj->FromUserName;
$ToUserName = $obj->ToUserName;
$time = time ();
// 返回XML数据包
$resultStr = sprintf ( $replyXML, $FromUserName, $ToUserName, $time, $videoArr );
return $resultStr;
}
/*
* 回复音乐消息
*/
function replyMusic($obj, $musicArr) {
$replyXML = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
</Music>
</xml>";
$FromUserName = $obj->FromUserName;
$ToUserName = $obj->ToUserName;
$time = time ();
// 返回XML数据包
$resultStr = sprintf ( $replyXML, $FromUserName, $ToUserName, $time, $musicArr ['Title'], $musicArr ['Description'], $musicArr ['MusicUrl'], $musicArr ['HQMusicUrl'], $musicArr ['ThumbMediaId'] );
return $resultStr;
}
/*
* 回复图文消息
*/
function replyNews($obj, $newsArr) {
$itemStr = "";
if (is_array ( $newsArr )) {
foreach ( $newsArr as $item ) {
$itemXML = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$itemStr .= sprintf ( $itemXML, $item ["Title"], $item ["Description"], $item ["PicUrl"], $item ["Url"] );
}
}
$replyXML = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
{$itemStr}
</Articles>
</xml> ";
$FromUserName = $obj->FromUserName;
$ToUserName = $obj->ToUserName;
$time = time ();
// 返回XML数据包
$resultStr = sprintf ( $replyXML, $FromUserName, $ToUserName, $time, count ( $newsArr ) );
return $resultStr;
}
?>
微信公众平台开发(3)封装全部基础接口源码