微信自定義菜單的創(chuàng)建/查詢/取消php示例代碼
微信公眾帳號 服務(wù)號可以使用 自定義菜單功能。之前在創(chuàng)建菜單時(shí)一直失敗,原因是$data 格式一直沒有傳正確,后來終于解決了。這里先記錄下 順便封裝了一個(gè)類,便于自定義菜單的管理。此類僅僅是自定義菜單的管理類,并未涉及微信自定義回復(fù)和菜單事件的代碼。
代碼如下
/** * @author LSH 2013-09-06 * * 微信自定義菜單的創(chuàng)建|查詢|取消 */ class weixinMenu { public static $appid = null; // 申請得到的appid public static $secret = null; // 申請得到的secret public static $getToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; public static $createMenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="; public static $selMenu ="https://api.weixin.qq.com/cgi-bin/menu/get?access_token="; public static $delMenu = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="; public static $opt = array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)', CURLOPT_FOLLOWLOCATION => 1, CURLOPT_AUTOREFERER =>1, CURLOPT_RETURNTRANSFER => true ); public $ACCESS_TOKEN = null; /** * 創(chuàng)建菜單 */ public function create() { $this->token(); $strMeau = '{ "button":[ { "type":"click", "name":"菜單左", "key":"V_MENU_LEFT" }, { "type":"click", "name":"菜單中", "key":"V_MENU_CENTER" }, { "type":"click", "name":"菜單右", "key":"V_MENU_RIGHT" }] }'; $ret = $this->HttpPost(self::$createMenu.$this->ACCESS_TOKEN,self::$opt, $strMeau); echo $ret; } /** * 查詢菜單 */ public function sel() { $this->token(); $ret = $this->HttpGet(self::$selMenu.$this->ACCESS_TOKEN,self::$opt); echo $ret; } /** * 取消菜單 */ public function del() { $this->token(); $ret = $this->HttpGet(self::$delMenu.$this->ACCESS_TOKEN,self::$opt); echo $ret; } /** * 獲取token */ private function token() { $tokenUrl = self::$getToken."&appid=".self::$appid."&secret=".self::$secret; $ret = $this->HttpGet($tokenUrl,self::$opt); $arrRet = json_decode($ret,true); $this->ACCESS_TOKEN = $arrRet['access_token']; } /** * POST 模式 * @param string $url post 的地址 * @param array $opt post 選項(xiàng) * @param array $post_data post 數(shù)據(jù) * @return mixed */ private function HttpPost($url, $opt = array(),$post_data) { $setopt = array( CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $post_data, ); if ( !empty($opt) ) { foreach ($opt as $key => $value) { $setopt[$key] = $value; } } $curl = curl_init($url); foreach ($setopt as $key => $value) { curl_setopt($curl, $key, $value ); } $responseText = curl_exec($curl); curl_close($curl); return $responseText; } /** * GET 方式 * @param stinrg $url GET 的url * @param array $opt GET 的選項(xiàng) * @return mixed */ private function HttpGet($url, $opt = array()) { $setopt = array( CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url ); if ( !empty($opt) ) { foreach ($opt as $key => $value) { $setopt[$key] = $value; } } $curl = curl_init($url); foreach ($setopt as $key => $value) { curl_setopt($curl, $key, $value ); } $responseText = curl_exec($curl); curl_close($curl); return $responseText; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PHP面向?qū)ο笕筇攸c(diǎn)學(xué)習(xí)(充分理解抽象、封裝、繼承、多態(tài))
PHP面向?qū)ο笕筇攸c(diǎn)學(xué)習(xí) 學(xué)習(xí)目標(biāo):充分理解抽象、封裝、繼承、多態(tài)2012-05-05學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)合成模式(composite)
這篇文章主要介紹了php設(shè)計(jì)模式中的合成模式,使用php實(shí)現(xiàn)合成模式,感興趣的小伙伴們可以參考一下2015-12-12php判斷類是否存在函數(shù)class_exists用法分析
這篇文章主要介紹了php判斷類是否存在函數(shù)class_exists用法,實(shí)例分析了PHP針對類是否存在進(jìn)行判斷的應(yīng)用,對于自動加載類以及類實(shí)例化之前的存在判斷來說都非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11PHP實(shí)現(xiàn)的生成唯一RequestID類完整示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的生成唯一RequestID類,結(jié)合完整實(shí)例形式分析了php唯一標(biāo)識符生成、session操作等相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-07-07php的SimpleXML方法讀寫XML接口文件實(shí)例解析
在php5中讀寫xml文檔是非常方便的,可以直接使用php的SimpleXML方法來快速解析與生成xml格式的文件,本文實(shí)例說明如下,需要的朋友可以參考下2014-06-06PHP實(shí)現(xiàn)單例模式建立數(shù)據(jù)庫連接的方法分析
這篇文章主要介紹了PHP實(shí)現(xiàn)單例模式建立數(shù)據(jù)庫連接的方法,結(jié)合實(shí)例形式分析了PHP單例模式的概念、原理及使用單例模式實(shí)現(xiàn)數(shù)據(jù)庫連接的相關(guān)操作技巧,需要的朋友可以參考下2020-02-02PHP 刪除一個(gè)目錄及目錄下的所有文件的函數(shù)代碼
PHP刪除一個(gè)目錄及目錄下的文件代碼,即刪除目錄或刪除文件。2010-05-05