萌萌哒小祝祝
2017-03-23 12:39
<?php
class wei{
public function index(){
$timestamp = $_GET['timestamp'];
$token = '15228990111';
$nonce = $_GET['nonce'];
$signature = $_GET['signature'];
$echostr = $_GET['echostr'];
$array=array($nonce,$timestamp,$token);
sort($array);
$str=implode('',$array);
$str=sha1($str);
if($str == $signature && $echostr){
echo $echostr;
exit;
}else{
$this->info();
}
}
public function info(){
//获得微信推送过来的 POST 数据
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
//处理消息类型 XML格式转换,并设置回复类型和内容
$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;
}
}
}
}
$wei=new wei();
$wei->index();
你那有个小写 TO to改成To
我是按照官方的文档里的demo写的,你最好还是用官方的试一试,改几个参数就行。然后在原有的基础上做其他的东西
PHP实现微信公众平台开发—提升篇
64947 学习 · 371 问题
相似问题