PHP實(shí)現(xiàn)微信公眾號(hào)企業(yè)號(hào)自定義菜單接口示例
本文實(shí)例講述了PHP實(shí)現(xiàn)微信公眾號(hào)企業(yè)號(hào)自定義菜單接口。分享給大家供大家參考,具體如下:
define(AppId, "wx666cae44xxxxxx2");//定義AppId,需要在微信公眾平臺(tái)申請(qǐng)自定義菜單后會(huì)得到
define(AppSecret, "d77026a714d443a01d0229xxxxxxxx");//定義AppSecret,需要在微信公眾平臺(tái)申請(qǐng)自定義菜單后會(huì)得到
include("menu.php");//引入微信類
$wechatObj = new Wechat();//實(shí)例化微信類
$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);//通過自定義函數(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":"分銷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":"精簡組團(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":"分銷A型",
"key":"fxa"
},
{
"type":"click",
"name":"分銷B型",
"key":"fxb"
},
{
"type":"click",
"name":"地接批發(fā)",
"key":"dj"
},
{
"type":"click",
"name":"精簡組團(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)容感興趣的讀者可查看本站專題:《PHP微信開發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP中json格式數(shù)據(jù)操作技巧匯總》及《PHP針對(duì)XML文件操作技巧總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP實(shí)現(xiàn)創(chuàng)建微信自定義菜單的方法示例
- php微信高級(jí)接口調(diào)用方法(自定義菜單接口、客服接口、二維碼)
- php微信開發(fā)之自定義菜單實(shí)現(xiàn)
- php微信開發(fā)之自定義菜單完整流程
- php微信開發(fā)自定義菜單
- 微信自定義菜單的創(chuàng)建/查詢/取消php示例代碼
- 微信利用PHP創(chuàng)建自定義菜單的方法
- php實(shí)現(xiàn)微信公眾平臺(tái)賬號(hào)自定義菜單類
- php實(shí)現(xiàn)微信公眾平臺(tái)賬號(hào)自定義菜單類
- php微信公眾號(hào)開發(fā)之二級(jí)菜單
相關(guān)文章
PHP實(shí)現(xiàn)的一致性哈希算法完整實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)的一致性哈希算法,以完整實(shí)例形式分析了PHP哈希算法的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
PHP獲取欄目的所有子級(jí)和孫級(jí)欄目的ID號(hào)示例
這篇文章主要介紹了PHP獲取欄目的所有子級(jí)和孫級(jí)欄目的ID號(hào)的具體實(shí)現(xiàn),需要的朋友可以參考下2014-04-04
PHP常用設(shè)計(jì)模式之委托設(shè)計(jì)模式
委托是對(duì)一個(gè)類的功能進(jìn)行擴(kuò)展和復(fù)用的方法。它的做法是:寫一個(gè)附加的類提供附加的功能,并使用原來的類的實(shí)例提供原有的功能,接下來通過本文給大家介紹PHP委托設(shè)計(jì)模式實(shí)例詳解,感興趣的朋友一起學(xué)習(xí)吧2016-02-02

