a、纯文本回复
上代码:
public function index()
{
//将timestamp,nonce,token按字典序排序
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = 'weixin';
$signature = $_GET['signature'];
//将排序之后的三个参数拼接之后用sha1加密
$array = array($timestamp, $nonce, $token);
sort($array);
$tmpstr = implode('', $array);
$tmpstr = sha1($tmpstr);
//将加密后的字符串与sianature进行对比,判断请求是否来自于微信
if ($tmpstr == $signature && $_GET['echostr']) {//第一次接入微信Api
echo $_GET['echostr'];
exit;
} else {
$this->responseMsg();
}
}
public function responseMsg()
{
//接收到的用户消息格式<xml>
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
// 接收到的消息格式
// <xml>
// <ToUserName><![CDATA[toUser]]></ToUserName>
// <FromUserName><![CDATA[FromUser]]></FromUserName>
// <CreateTime>123456789</CreateTime>
// <MsgType><![CDATA[event]]></MsgType>
// <Event><![CDATA[subscribe]]></Event>
// </xml>
//把接收到的xml格式数据包转化为对象格式
$postObj = simplexml_load_string($postArr);
// 被动回复用户消息格式
// <xml>
// <ToUserName><![CDATA[toUser]]></ToUserName>
// <FromUserName><![CDATA[fromUser]]></FromUserName>
// <CreateTime>12345678</CreateTime>
// <MsgType><![CDATA[text]]></MsgType>
// <Content><![CDATA[你好]]></Content>
// </xml>
if (strtolower($postObj->MsgType) == 'event') {//判断是否为订阅的事件推送
if (strtolower($postObj->Event) == 'subscribe') {//判断是否为关注的事件推送
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
$content = '你好:'.$postObj->FromUserName.'!欢迎关注我们的微信公众号:'.$postObj->ToUserName;;
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
echo $info;
}
}elseif(strtolower($postObj->MsgType == 'text')){
if($postObj->Content == 'hello'){
$content = '我爱你!';
}elseif($postObj->Content == 'baidu'){
$content = '<a href="http://www.baidu.com"> 百度</a>';
}
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info = sprintf($template,$toUser,$fromUser,$time,$msgType,$content);
echo $info;
}
}
}
@$ b、单图文回复与多图文::区别,foreach个数
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'news';
$arrdata = [
[
'Title' => '端午节促销',
'Description' => '庆祝端午节,只要99只要99,就这一天',
'PicUrl' => 'http://hiphotos.baidu.com/zhixin/abpic/item/d1571724ab18972bbb8d648ce4cd7b899f510a85.jpg',
'Url' => 'http://www.baidu.com'
],
[
'Title' => '元宵节促销',
'Description' => '庆祝元宵节,只要9.9只要9.9,就这一天',
'PicUrl' => 'https://www.baidu.com/img/bd_logo1.png',
'Url' => 'http://www.mnxiao.top'
],
[
'Title' => '元宵节促销',
'Description' => '庆祝元宵节,只要9.9只要9.9,就这一天',
'PicUrl' => 'https://www.baidu.com/img/bd_logo1.png',
'Url' => 'http://www.mnxiao.top'
],
];
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>" . count($arrdata) . "</ArticleCount>
<Articles>";
foreach ($arrdata as $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>
</xml>";
echo sprintf($template, $toUser, $fromUser, $time, $msgType);
}