您好,登錄后才能下訂單哦!
小編給大家分享一下PHP如何實現微信公眾號企業(yè)號自定義菜單接口,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體如下:
define(AppId, "wx666cae44xxxxxx2");//定義AppId,需要在微信公眾平臺申請自定義菜單后會得到 define(AppSecret, "d77026a714d443a01d0229xxxxxxxx");//定義AppSecret,需要在微信公眾平臺申請自定義菜單后會得到 include("menu.php");//引入微信類 $wechatObj = new Wechat();//實例化微信類 $creatMenu = $wechatObj->creatMenu();//創(chuàng)建菜單
微信類(menu.php)代碼
<? class Wechat { private function getAccessToken() //獲取access_token { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppId."&secret=".AppSecret; $data = getCurl($url);//通過自定義函數getCurl得到https的內容 $resultArr = json_decode($data, true);//轉為數組 return $resultArr["access_token"];//獲取access_token } public function creatMenu()//創(chuàng)建菜單 { $accessToken = $this->getAccessToken();//獲取access_token $menuPostString = '{//構造POST給微信服務器的菜單結構體 "button":[ { "name":"產品介紹", "sub_button":[ { "type":"view", "name":"分銷A型", "url":"http://www.yourwebname.com/fenxiao/jianjie/soft.html" }, { "type":"view", "name":"分銷B型", "url":"http://www.yourwebname.com/fenxiaob/jianjie/soft.html" },{ "type":"view", "name":"地接批發(fā)", "url":"http://www.yourwebname.com/dijie/jianjie/soft.html" },{ "type":"view", "name":"精簡組團", "url":"http://www.yourwebname.com/zutuan/jianjie/soft.html" },{ "type":"view", "name":"直客網站", "url":"http://www.yourwebname.com/tripal/jianjie/soft.html" }] }, { "name":"申請試用", "sub_button":[ { "type":"click", "name":"分銷A型", "key":"fxa" }, { "type":"click", "name":"分銷B型", "key":"fxb" }, { "type":"click", "name":"地接批發(fā)", "key":"dj" }, { "type":"click", "name":"精簡組團", "key":"zutuan" }, { "type":"click", "name":"直客網站", "key":"zhike" } ] }, { "name":"博縱在線", "sub_button":[ { "type":"view", "name":"企業(yè)介紹", "url":"http://www.yourwebname.com/about.html" }, { "type":"view", "name":"公司新聞", "url":"http://www.yourwebname.com/news/company/" }, { "type":"view", "name":"聯系我們", "url":"http://www.yourwebname.com/contact.html" } ] } ] }'; $menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accessToken;//POST的url $menu = dataPost($menuPostString, $menuPostUrl);//將菜單結構體POST給微信服務器 } } function getCurl($url){//get https的內容 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不輸出內容 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $result = curl_exec($ch); curl_close ($ch); return $result; } function dataPost($post_string, $url) {//POST方式提交數據 $context = array ('http' => array ('method' => "POST", 'header' => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*", 'content' => $post_string ) ); $stream_context = stream_context_create ( $context ); $data = file_get_contents ( $url, FALSE, $stream_context ); return $data; } ?>
看完了這篇文章,相信你對“PHP如何實現微信公眾號企業(yè)號自定義菜單接口”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。