在公众平台注册订阅号后
在订阅号的开发者配置好基本配置
再通过https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421137522
这个链接接入测试号 进行代码自动回复
进入测试号后 要填入对应的url与token令牌
<?php
$nonce = $_GET['nonce'];
$token = 'daijiahao';
$timestamp = $_GET['timestamp'];
$echostr = $_GET['echostr'];
$signature = $_GET['signature'];
//形成数组,然后按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密,然后与signature进行检验
$str = sha1(implode($array));
if ($str == $signature && $echostr) {
header('content-type:text');
//第一次接入微信API接口时候验证合法性
echo $echostr;
exit;
} else {
}
responseMsg();
function responseMsg()
{
//1.获取到微信推送过来post数据(xml格式)
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
//2.处理消息类型,并设置回复类型和内容
$postObj = simplexml_load_string($postArr);
//判断该数据包是否是订阅de事件推送
if (strtolower($postObj->MsgType) == 'event') {
//如果是关注 subscribe事件
if (strtolower($postObj->Event) == 'subscribe') {
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
$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;
}
}
}
token配置成功后就可以把wxsamp.php代码清空 写新的方法
评论已关闭