<?php
namespace?app\index\controller;
class?Index
{
????public?function?index()
????{
????????if?(isset($_GET['echostr']))?{
????????????//獲得參數(shù)?signature?nonce?token?timestamp?echostr
????????????$nonce?=?$_GET['nonce'];
????????????$token?=?'weixin';
????????????$timestamp?=?$_GET['timestamp'];
????????????$echostr?=?$_GET['echostr'];
????????????$signature?=?$_GET['signature'];
????????????//形成數(shù)組,然后按字典序排序
????????????$array?=?array();
????????????$array?=?array($nonce,?$timestamp,?$token);
????????????sort($array);
????????????//拼接成字符串,sha1加密?,然后與signature進行校驗
????????????$str?=?sha1(implode($array));
????????????if?($str?==?$signature?&&?$echostr)?{
????????????????//第一次接入weixin?api接口的時候
????????????????echo?$echostr;
????????????????exit;
????????????}
????????}?else?{
????????????$this->responseMsg();
????????}
????}
????public?function?responseMsg(){
????????//1.獲取微信推送過來的post數(shù)據(jù)(xml格式)
????????$postArr?=?file_get_contents("php://input");
????????libxml_disable_entity_loader(true);
????????$postObj?=?simplexml_load_string($postArr,?'SimpleXMLElement',?LIBXML_NOCDATA);
//????????print_r($postObj);
????????if(strtolower($postObj->MsgType)?==?'event'){
????????????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;
????????????}
????????}
????}
????
????本人用的tp5框架
2020-01-02
請問你的出來了么?我也遇到同樣的問題了