問題描述:
繼上一篇的《自定義微信公眾號的個性化菜單欄》的文章,為了“根據不同用戶標簽顯示不同的功能菜單並且點擊菜單跳轉到外網”這個功能需求,研究了一天的微信開發文檔關於個性化菜單,消息接口事件推送,用戶標簽的問題,將自己在開發過程中遇到的問題分享出來,讓更多的小伙伴少走彎路。
梳理問題:
其一 :接觸過微信公眾號開發的小伙伴都知道,微信公眾號(認證服務號)的后台是給用戶提供有自定義菜單設置,並且在那里可以設置菜單並且設置url連接來跳轉到網頁
其二:我們可以通過微信提供的自定義菜單創建接口,(如何獲取AccessToken請參考上一篇文章)
//生成自定義菜單(默認菜單) public function createMenu(){ $access_token = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token; //拼裝要生成的菜單 $array = array( 'button'=>array( //第一個一級菜單 // array( // 'type'=>'click', // 'name'=>'hello', // 'key' =>'front', // // ), array( "type"=>"view", "name"=> 'world', "url"=>"http://www.baidu.com" ), //第二個一級菜單 // array( // 'type'=>'click', // 'name' => urlencode($data['second_name']), // 'key' =>'behind', // ), ), ); //轉化成json的格式 $arrayJson = urldecode(json_encode($array)); $this->http_curl($url,'post','json',$arrayJson); }
CURL請求:
public function http_curl($url,$type='get',$res='json',$arr=''){ //1.初始化 $ch = curl_init(); //2.設置參數 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); if( $type == 'post' ){ //如果是post請求的話,設置post的一些參數 curl_setopt($ch , CURLOPT_POST , 1); curl_setopt($ch , CURLOPT_POSTFIELDS, $arr); } //3.執行 $result = curl_exec($ch); if( curl_errno($ch)){ //打印錯誤日志 var_dump(curl_error($ch)); } if( $res == 'json' ){ //將json轉化成數組的形式 $result = json_decode($result , TRUE); } //4.關閉 curl_close($ch); return $result; }
說明:這里的菜單是定義的二級菜單,定義二級菜單的作用是可以通過其url參數實現點擊跳轉(特別提示:官方文檔中並沒有說明在沒有一級菜單的情況下不允許定義二級菜單,所以通過自定義接口我們可以直接創建二級菜單)
小插曲:開始按照官方文檔指示,並沒有創建二級菜單,而是創建一級菜單,然而一級菜單的type事件跟二級的type事件不一樣,一級菜單需要開啟公眾號后台的服務器配置,並且需要配置相關服務器接口,微信根據所配置的接口,將用戶操作的內容發送到該接口,一級菜單的click事件綁定的EventKey參數可以用來在接口中識別是點擊了哪個自定義菜單,可以根據EventKey的值來進行相關業務邏輯的處理。
其三:創建個性化菜單
根據微信官方文檔說明:
個性化菜單匹配規則說明:
個性化菜單的更新是會被覆蓋的。 例如公眾號先后發布了默認菜單,個性化菜單1,個性化菜單2,個性化菜單3。那么當用戶進入公眾號頁面時,將從個性化菜單3開始匹配,如果個性化菜單3匹配成功,則直接返回個性化菜單3,否則繼續嘗試匹配個性化菜單2,直到成功匹配到一個菜單。 根據上述匹配規則,為了避免菜單生效時間的混淆,決定不予提供個性化菜單編輯API,開發者需要更新菜單時,需將完整配置重新發布一輪。
//創建個性化匹配菜單 public function characterMenu() { $access_token = $this->getAccessToken(); $url='https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token='.$access_token; $array = array( 'button'=>array( array( "type"=>"view", "name"=> 'hello', "url"=>"要跳轉的url" ), array( "type"=>"view", "name"=> 'world', "url"=>"要跳轉的url" ), ), "matchrule"=>array( "tag_id"=>100//標簽匹配,微信官方放回給你的標簽id ) ); //轉化成json的格式 $arrayJson = urldecode(json_encode($array)); $this->http_curl($url,'post','json',$arrayJson); }
說明:個性化菜單的創建也是直接創建的二級菜單,如何創建標簽id接下來說明(申請標簽應在創建個性化菜單之前)
matchrule的匹配規則項我們在這里只使用tag_id標簽匹配,其他匹配項請參考微信開放文檔(https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Personalized_menu_interface.html)
其四:創建用戶標簽
創建用戶標簽也非常簡單,直接上代碼
/** * 生成用戶標簽接口 **/ public function createTag() { $access_token = $this->getAccessToken(); $url='https://api.weixin.qq.com/cgi-bin/tags/create?access_token='.$access_token; $data['tag']['name']=urlencode('管理員'); $res=$this->http_curl($url,'post','json',json_encode($data)); dump($res); //成功返回的參數包括tag_id }
其五:為用戶添加標簽
該接口是批量為用戶添加標簽,參數為數組形式,里面只包含用戶在該公眾號下的唯一的openid值
//批量為用戶打上微信公眾號的標簽類型 public function pasteTag($arr_openid) { $access_token=$this->getAccessToken(); $url='https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token='.$access_token; $data['openid_list']=$arr_openid; $data['tagid']=你的標簽id; $data=$this->http_curl($url,'post','json',json_encode($data)); dump($data); }
在此,當用戶訪問該公眾號的時候,根據用戶身上的標簽,會顯示不同的菜單欄內容,實現個性化菜單的展示功能
Additional:
為用戶取消標簽:
openid為數組形式,數組里面是你要取消的該公眾號下的唯一的用戶的openid
//為用戶取消標簽 public function cancelTag($openid,$tagid) { $access_token = $this->getAccessToken(); $url='https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token='.$access_token; $data['openid_list']=$openid; $data['tagid']=$tagid; $res = $this->http_curl($url,'post','json',json_encode($data)); wxlogging('cancel_user_tag',json_encode($res)); }
獲取該公眾號創建的標簽:
//獲取微信公眾號所創建的標簽 public function getTag() { $access_token=$this->getAccessToken(); $url='https://api.weixin.qq.com/cgi-bin/tags/get?access_token='.$access_token; $data=$this->http_curl($url); dump($data); }
獲取某個標簽下的用戶:
//獲取某個標簽下面的粉絲openid public function getFans() { $access_token=$this->getAccessToken(); $url='https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token='.$access_token; $data['tagid']=100; $data=$this->http_curl($url,'post','json',json_encode($data)); dump($data); }
刪除所有菜單
public function delete() { $access_token=$this->getAccessToken(); $url='https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$access_token; $res=$this->http_curl($url,'get','json'); dump($res); }