thinkphp5.1微信公眾號封裝之url配置驗證及被動消息回復


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;
 
}
 
 
}
 
}
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM