<?php
class IndexAction extends Action {
public function _construct(){
}
public function index(){
$nonce=$_GET['nonce'];
$token='zhongmei';
$timestamp=$_GET['timestamp'];
$signature=$_GET['signature'];
$echostr=$_GET['echostr'];
$array=array();
$array=array($nonce,$timestamp,$token);
sort($array);
$str=sha1(implode($array));
if($str==$signature && $echostr){
echo $echostr;
exit;
}else {
$this->reponseMsg();
}
}
public function reponseMsg(){
//1.获取到微信推送过来post数据(xml格式)
$postArr = $GLOBALS('HTTP_RAW_POST_DATA');
//2.处理消息类型,并设置回复类型和内容
$postObj = simplexml_load_string($postArr);//转化成对象
//$postObj->ToUserName='';
//$postObj->FromUserName='';
//$postObj->CreateTime ='';
//$postObj->MsgType='';
//$postObj->Event='';
//判断该数据包是否是订阅的时间推送
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;
}
}
if( strtolower( $postObj->MsgType) == 'text'){
if( strtolower($postObj->Content == 'zhongmei') ){
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$fromUser=$postObj->ToUserName;
$toUser=$postObj->FromUserName;
$time=time();
$content='zhongmei is good';
$msgType='text';
echo sprintf($template,$toUser,$fromUser,$time,$msgType,$content);
}
}
}
}首先微信中服务器配置已经更改为自己的了,token也改了。但在启用服务器配置并订阅后什么推送消息都没有,输入zhongmei后也没有返回值,只返回该公众号暂时无法提供服务,请稍后再试。请帮忙看看哪里出问题了,谢谢!
1 回答
- 1 回答
- 0 关注
- 2538 浏览
添加回答
举报
0/150
提交
取消