求助 自动推送不显示 测不出来哪出问题
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
//获得参数 signature nance token timestamp echostr
$nonce = $_GET['nonce'];
$token = 'cyts';
$timestamp = $_GET['timestamp'];
$echostr = $_GET['echostr'];
$signature = $_GET['signature'];
//形成数组然后用字典排序
$array = array($nonce,$token,$timestamp);
sort($array);
//拼接字符串,加密,然后与sigature进行校验
$str = sha1(implode($array));
if ($str == $signature && $echostr) {
//第一次介入weixin API接口的时候验证
echo $echostr;
exit;
}else{
$this->reponseMsg();
}
}
//接收时间推送并回复
public function reponseMsg(){
//1.获取到微信推送过来POST数据(xml格式)
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
//2处理消息类型,并设置回复类型和内容
// <xml>
// <ToUserName><![CDATA[toUser]]></ToUserName>
// <FromUserName><![CDATA[FromUser]]></FromUserName>
// <CreateTime>123456789</CreateTime>
// <MsgType><![CDATA[event]]></MsgType>
// <Event><![CDATA[subscribe]]></Event>
// </xml>
// $postObj = simplexml_load_string($postar);
// $postObj->ToUseName = '';
// $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 = '欢迎关注我的微信公众账号';
file_put_contents('1.php', $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;
}
}
}
}