<?php
header("Content-type: text/html; charset=utf-8");
$nonce = $_GET['nonce'];
$token = 'winxin';
$timestamp = $_GET['timestamp'];
if (isset($_GET['echostr'])) {
if (bindServerCheck()) {
echo $_GET['echostr'];
}
exit();
}
responseMsg();
//消息回复
function responseMsg() {
//1.获取到微信推送过来post数据(xml格式)
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
//libxml_disable_entity_loader(true);
//2.处理消息类型,并设置回复类型和内容
$postObj = simplexml_load_string($postArr, 'SimpleXMLElement', LIBXML_NOCDATA);
//判断该数据包是否是订阅de事件推送
if (strtolower($postObj->MsgType) == 'event') {
//如果是关注 subscribe事件
if (strtolower($postObj->Event) == 'subscribe') {
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
$content = '欢迎关注我的微信公众号: 微信公众号。';
//修改为
if (is_utf8($content)) {
$content = $content;
} else {
$content = iconv('gb2312', 'UTF-8//IGNORE', $content);
}
$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;
}
}
if (strtolower($postObj->MsgType) == 'text') {
//如果是关注 subscribe事件
// if (strtolower($postObj->Event) ==
php 微信公众号开发之 二 消息自动回复
最新推荐文章于 2024-09-15 23:19:45 发布