tp5.1在接入公众号的时候,文件引入的方式都是以命名空间的方式,所以先将微信在开发文档中的demo下载下来,然后修改为通过命名空间的方式引入,在extend目录下新建一个wetch目录,将几个文件放进去,由于直接下载下来的pkcs7Encoder文件中包含了两个,故将其拆分为pkcs7Encoder和prpcrypt两个文件,同时里面有出现方法名称和类名相同,因为此方式只是加载需要用的配置参数,故而将次方法改为构造方法__construct,同时各个文件都改为命名空间的方式加载
接入微信公众号控制器代码如下:
namespace app\wetch\controller;
use think\Controller;
use think\Request;
use think\facade\Config;
use wetch\wxBizMsgCrypt;
class Inter extends Controller{
protected $signature;
protected $timestamp;
protected $nonce;
protected $token;
protected $echoStr;
protected $msgsignature;
protected $appid;
protected $EncodingAESKey;
protected $AppSecret;
protected $ToUserName;
protected $FromUserName;
//get数据处理
public function __construct(Request $request){
$data = $request->param();
$this->signature = isset($data['signature'])?$data['signature']:false;
$this->timestamp = isset($data['timestamp'])?$data['timestamp']:false;
$this->nonce = isset($data['nonce'])?$data['nonce']:false;
$this->echoStr = isset($data['echostr'])?$data['echostr']:false;
$this->msgsignature = isset($data['msg_signature'])?$data['msg_signature']:false;
$this->token = Config::get('token');
$this->appid = Config::get('appid');
$this->EncodingAESKey = Config::get('EncodingAESKey');
$this->AppSecret = Config::get('AppSecret');
}
public function respons(Request $request){
//验证配置URL
if($this->echoStr!==false){
$result = $this->checkSignature($this->timestamp,$this->nonce,$this->token,$this->signature);
if($result == true){
echo $this->echoStr;
exit;
}
}else{
//接收消息
$postData = file_get_contents("php://input");
$this->loadxml($postData);
}
}
//xml消息解密
private function loadxml($postData){
$pc = new wxBizMsgCrypt($this->token,$this->EncodingAESKey,$this->appid);
$datacode = $pc->decryptMsg($this->msgsignature,$this->timestamp,$this->nonce,$postData,$msg);
$xmlObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->send($xmlObj);
}
//被动消息回复
private function send($xmlObj){
$MsgType = $xmlObj->MsgType;
$Content = $xmlObj->Content;
$MsgId = $xmlObj->MsgId;
$pc = new wxBizMsgCrypt($this->token,$this->EncodingAESKey,$this->appid);
//关键词回复
if($MsgType == "text"){
switch($Content){
case "你好":
$text = "很好,非常好";
break;
case "你是谁":
$text = "你猜猜看";
break;
default:
$text = "来呀来呀,看过来";
}
$message = $this->sendText($xmlObj,$text);
}elseif($MsgType == "image"){
$image = $xmlObj->MediaId;
$message = $this->sendImg($xmlObj,$image);
}elseif($MsgType == "voice"){
$voice = $xmlObj->MediaId;
$message = $this->sendVoice($xmlObj,$voice);
}elseif($MsgType == "event"){
if($xmlObj->Event == "subscribe"){
//关注事件
$message = $this->sendTexts($xmlObj);
}else{
if($xmlObj->Event == "unsubcribe"){
//取消关注事件
}
}
}else{
$text = "欢迎关注";
$message = $this->sendText($xmlObj,$text);
}
$errorCode = $pc->encryptMsg($message, $this->timestamp, $this->nonce, $encryptMsg);
echo $encryptMsg;
}
//回复文字消息
public function sendText($obj,$content){
$xml = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";
$result = sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$content);
return $result;
}
//回复图片消息
public function sendImg($xmlobj,$image){
$xml = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
$result = sprintf($xml,$xmlobj->FromUserName,$xmlobj->ToUserName,time(),$image);
return $result;
}
//回复语音消息
public function sendVoice($obj,$voice){
$xml = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>
</xml>';
$result = sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$voice);
return $result;
}
//回复图文消息
public function sendTexts($obj){
//需要的图片
$pic_url = '';
//需要跳转的链接
$url = '';
$xml = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[news_title1111111]]></Title>
<Description><![CDATA[nwes_description111111111]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
<item>
<Title><![CDATA[news_title2222222]]></Title>
<Description><![CDATA[nes_description22222222]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>';
$result = sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$pic_url,$url,$pic_url,$url);
return $result;
}
//校验配置签名
private function checkSignature($timestamp,$nonce,$token,$signature){
$tmpArr = [$timestamp,$nonce,$token];
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
return true;
}else{
return false;
}
}
}