官方消息回复链接
不知道官方XML模板为什么没有 FuncFlag标签
回复纯文本消息
1.关键字回复
2.在回复的纯文本中设置链接
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[content]]></Content>
<FuncFlag>0</FuncFlag>
</xml>
注:纯文本回复的时候链接要加上http或者https
case 'acfun': $content = "<a href='http://www.acfun.cn'>( ⊙ o ⊙ )!活捉A站基佬</a>"; break;
回复单图文消息
关键字回复
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[title1]]></Title>
<Description><![CDATA[description1]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml>
FuncFlag
位0x0001被标志时,星标刚收到的消息。
回复多图文消息
注:子图文个数不能超过10个,如下图:
- 回复视频消息
- 回复语音消息
- 回复音乐消息
代码实现如图:
<?php
$testObj = new Test();
if(!empty($_GET['echostr'])){
$testObj->valid();
}else{
$testObj->responseMsg();
}
exit;
class Test
{
/**
* 绑定url、token信息
*/
public function valid(){
$echoStr = $_GET["echostr"];
if ($this->checkSignature()) {
ob_clean();
echo $echoStr;
}
exit();
}
/**
* 检查签名,确保请求是从微信发过来的
*/
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = "lingsg";//与在微信配置的token一致,不可泄露
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function responseMsg(){
//验证签名
if ($this->checkSignature()){
$postArr = $GLOBALS["HTTP_RAW_POST_DATA"];
$postObj = simplexml_load_string( $postArr );
/****************************************************
* 关注事件触发的推送 *
* **************************************************
*/
if( strtolower( $postObj->MsgType ) == 'event' ){
if( strtolower( $postObj->Event == 'subscribe' ) ){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
$content = '微信公众号:'.$fromUser.",\n你的微信openid是:".$toUser;
$info = $this->txtFormatForXml($toUser, $fromUser, $time, $msgType, $content);
echo $info;
}
}
/****************************************************
* 回复纯文本 *
* **************************************************
*/
if( strtolower( $postObj->MsgType ) == 'text'){
switch ( $postObj->Content ) {
case '菜单':
$content = '正在开发中……敬请期待!';
break;
case 'acfun':
$content = "<a href='http://www.acfun.cn'>( ⊙ o ⊙ )!活捉A站基佬</a>";
break;
case '微信开发':
$content=array(
array(
'title' => '微信公众号发送红包',
'description' => '微信红包准备条件,以及发送红包的实例',
'picUrl' => 'http://chuantu.biz/t5/120/1498705137x2890171516.jpg',
'url' => 'http://blog.csdn.net/qq_31617637/article/details/71972281'
)
);
break;
default:
$content = $postObj->Content;
break;
}
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
if(is_array($content)){
$info = $this->picFormatForXml($toUser, $fromUser, $time, $content);
} else {
$info = $this->txtFormatForXml($toUser, $fromUser, $time, $msgType, $content);
}
echo $info;
}
}
}
/*
* 纯文本模板解析
* */
private function txtFormatForXml($toUser, $fromUser, $time, $msgType, $content ) {
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$info = sprintf( $template, $toUser, $fromUser, $time, $msgType, $content );
return $info;
}
/*
* 多图文模板解析
* $msgType 为news
*
* */
private function picFormatForXml($toUser, $fromUser, $time, $content) {
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>";
foreach ($content as $key => $value){
$template .= "<item>
<Title><![CDATA[".$value['title']."]]></Title>
<Description><![CDATA[".$value['description']."]]></Description>
<PicUrl><![CDATA[".$value['picUrl']."]]></PicUrl>
<Url><![CDATA[".$value['url']."]]></Url>
</item>";
}
$template .= "</Articles>
<FuncFlag>0</FuncFlag>
</xml>";
$info = sprintf( $template, $toUser, $fromUser, $time, 'news');
return $info;
}
}
?>