public function index(){
$data=input('get.');
if(!$data)
{
throw new Exception("data无数据");
}
// $data=['d','e','rr','4444'];//test
// $s=implode(',',$data);
$s='接收到的信息为:';
foreach ($data as $key=>$v){
$s.=$key.':'.$v.';';
}
//查看微信传输的数据
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $s.'\n');
fclose($myfile);
//加密验证:微信用的sha1加密,加密后返回get中的signature参数
$timestamp=$data['timestamp'];
$nonce=$data['nonce'];
$token='poower';// 与微信服务器配置中放token保持一致
$signature=$data['signature'];
$array=array($timestamp,$nonce,$token);
sort($array);//字典排序
$str=implode('',$array);
$strs=sha1($str);
if($strs==$signature && $data['echostr']) {
echo $data['echostr'];
//}elseif(simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'])->MsgType=='event'){//事件判断
}else{
$this->responseMsg();
}
}
public function responseMsg(){
//1、获取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($postArr);
$s1='vvvv'.$postObj->FromUserName;
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $s1.'\n');
fclose($myfile);
//$postObj->ToUserName;
//判断该数据包是否是订阅事件的推送
if(strtolower($postObj->MsgType) == 'event'){
//如果是关注subscribe事件
if(strtolower($postObj->Event)=='subscribe'){
//回复用户消息
$toUser=$postObj->FromUserName;
$fromUser=$postObj->ToUserName;
$time=time();
$msgType='text';
$content='欢迎关注我们的微信公众账号'.$fromUser;
$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);
$myfile2 = fopen("newfile2.txt", "w") or die("Unable to open file!");
fwrite($myfile2, $info);
fclose($myfile2);
echo $info;
}
}
}两个文件相当于断点,数据均正常,就是最后一步出问题了:newfile2数据:<xml> <ToUserName>< ![CDATA[oDSqD08TZ9VBB5Dk9tPG7pC93IrI] ]></ToUserName> <FromUserName>< ![CDATA[gh_72d52b9a22ac] ]></FromUserName> <CreateTime>1513960368</CreateTime> <MsgType>< ![CDATA[text] ]></MsgType> <Content>< ![CDATA[娆㈣繋鍏虫敞鎴戜滑鐨勫井淇″叕浼楄处鍙穏h_72d52b9a22ac] ]></Content> </xml>