《PHP教程:PHP實(shí)現(xiàn)微信公眾號(hào)企業(yè)號(hào)自定義菜單接口示例》要點(diǎn):
本文介紹了PHP教程:PHP實(shí)現(xiàn)微信公眾號(hào)企業(yè)號(hào)自定義菜單接口示例,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
本文實(shí)例講述了PHP實(shí)現(xiàn)微信公眾號(hào)企業(yè)號(hào)自定義菜單接口.分享給大家供大家參考,具體如下:PHP教程
define(AppId, "wx666cae44xxxxxx2");//定義AppId,需要在微信公眾平臺(tái)申請(qǐng)自定義菜單后會(huì)得到 define(AppSecret, "d77026a714d443a01d0229xxxxxxxx");//定義AppSecret,需要在微信公眾平臺(tái)申請(qǐng)自定義菜單后會(huì)得到 include("menu.php");//引入微信類(lèi) $wechatObj = new Wechat();//實(shí)例化微信類(lèi) $creatMenu = $wechatObj->creatMenu();//創(chuàng)建菜單
微信類(lèi)(menu.php)代碼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);//通過(guò)自定義函數(shù)getCurl得到https的內(nèi)容 $resultArr = json_decode($data, true);//轉(zhuǎn)為數(shù)組 return $resultArr["access_token"];//獲取access_token } public function creatMenu()//創(chuàng)建菜單 { $accessToken = $this->getAccessToken();//獲取access_token $menuPostString = '{//構(gòu)造POST給微信服務(wù)器的菜單結(jié)構(gòu)體 "button":[ { "name":"產(chǎn)品介紹", "sub_button":[ { "type":"view", "name":"分銷(xiāo)A型", "url":"http://www.yourwebname.com/fenxiao/jianjie/soft.html" }, { "type":"view", "name":"分銷(xiāo)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":"精簡(jiǎn)組團(tuán)", "url":"http://www.yourwebname.com/zutuan/jianjie/soft.html" },{ "type":"view", "name":"直客網(wǎng)站", "url":"http://www.yourwebname.com/tripal/jianjie/soft.html" }] }, { "name":"申請(qǐng)?jiān)囉?, "sub_button":[ { "type":"click", "name":"分銷(xiāo)A型", "key":"fxa" }, { "type":"click", "name":"分銷(xiāo)B型", "key":"fxb" }, { "type":"click", "name":"地接批發(fā)", "key":"dj" }, { "type":"click", "name":"精簡(jiǎn)組團(tuán)", "key":"zutuan" }, { "type":"click", "name":"直客網(wǎng)站", "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":"聯(lián)系我們", "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);//將菜單結(jié)構(gòu)體POST給微信服務(wù)器 } } function getCurl($url){//get https的內(nèi)容 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不輸出內(nèi)容 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方式提交數(shù)據(jù) $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; } ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP微信開(kāi)發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP中json格式數(shù)據(jù)操作技巧匯總》及《PHP針對(duì)XML文件操作技巧總結(jié)》PHP教程
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助.PHP教程
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/260.html