日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

公众号自定义图文消息推送(2)

發布時間:2025/5/22 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 公众号自定义图文消息推送(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

這部分調試了很久,因為在新用戶掃描了好友分享的二維碼之后需要分發兩條消息,一條是自動推送給當前用戶歡迎的消息,另一條是推送給二維碼主人的消息。調試了很久都不能同時分發(也許是我的邏輯或者方法有問題吧【尷尬臉】)。后來去谷歌了一下,好像不能分發兩條或者多條消息,只能分發一條消息,所以這部分需要用到客服消息接口。

不多說,先看代碼

  • 首先要去配置公眾號的服務器配置,用于接收微信的推送消息 參考文檔(https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140543)
    注意:在啟用了服務器配置過后,原有的自定義菜單功能等將不能使用了,所以得編寫菜單功能
    參考文檔(https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013)
  • public function customMenu(){//組裝菜單數據$data = ' {"button":[{ "type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC"},{"name":"菜單","sub_button":[{ "type":"view","name":"搜索","url":"http://www.soso.com/"},{"type":"miniprogram","name":"wxa","url":"http://mp.weixin.qq.com","appid":"wx286b93c14bbf93aa","pagepath":"pages/lunar/index"},{"type":"click","name":"贊一下我們","key":"V1001_GOOD"}]}]}';$access_token = $this->getWeiXinToken();$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token";$getData = $this->httpPostFunc($url, $data, 'POST');var_dump($getData);
  • 驗證消息是否來自微信后臺(主要是驗證echostr參數)
  • private $appId;private $appSecret;/*** weChat constructor.*/public function __construct() {parent::__construct();$this->appId = 'xxxxxxx';$this->appSecret = 'xxxxxxxx';$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));//這里是框架自帶的緩存}/*** 參數signature、nonce、timestamp驗證消息是否來自微信后臺,驗證成功時返回echostr給微信后臺服務器,已確定接口配置成功*/public function index(){define("TOKEN", "xxxx");$echostr = $this->input->get('echostr',true);if (!$echostr) {$this->responseMsg();}else{$this->valid();}}private function valid(){$echoStr = $_GET["echostr"];if($this->checkSignature()){echo $echoStr;exit;}}/*** 驗證簽名* @return bool*/private function checkSignature(){$signature = $_GET["signature"];$timestamp = $_GET["timestamp"];$nonce = $_GET["nonce"];$token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);sort($tmpArr);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}}

    3.消息推送方法responseMsg,

    private function responseMsg(){$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if (!empty($postStr)){$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);$TYPE = trim($postObj->MsgType);$EventKey = trim((string)$postObj->EventKey);$keyArray = explode("_", $EventKey);if (count($keyArray) && count($keyArray) != 1) {//掃好友分享的二維碼關注,未關注者關注后推送事件$getContent = $this->getAndSaveUserInfo($postObj, $keyArray[1]);//拿到用戶的openId和userId,再拿到用戶的詳細信息,并將個人微信信息保存至數據庫}switch ($TYPE){case "text":$resultStr = $this->receiveText($postObj);break;case "event":if($postObj->EventKey == 'V1001_wap_create_1'){//點擊生成二維碼圖片$resultStr = $this->receiveText($postObj);}else{$resultStr = $this->receiveEvent($postObj);}if($getContent){//是否新關注者,并調用客服接口給二維碼主人發送消息$this->replyCustomer($getContent['openId'], $getContent['content']);}break;default:$resultStr = "";break;}echo $resultStr;}else {echo "";exit;}}

    4.在方法receiveText和receiveEvent中主要是組裝xml數據,主要請參考文檔(https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140453)
    方法replyCustomer主要是給二維碼主人或者其他人發消息,參考文檔(https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547)

    private function replyCustomer($touser,$content){$access_token = $this->getWeiXinToken();;$data = '{"touser":"'.$touser.'","msgtype":"text","text":{"content":"'.$content.'"}}';$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token ;$result = $this->httpPostFunc($url, $data, 'POST');$data = json_decode($result);return $data;}

    這是這個方法發的消息

    轉載于:https://my.oschina.net/Shinsg/blog/1053937

    總結

    以上是生活随笔為你收集整理的公众号自定义图文消息推送(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。