这篇文章主要为大家展示了“如何使用php实现微信自定义菜单创建/查询/取消功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用php实现微信自定义菜单创建/查询/取消功能”这篇文章吧。
代码如下
/** * @author LSH 2013-09-06 * * 微信自定义菜单的创建|查询|取消 */ 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; /** * 创建菜单 */ 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 选项 * @param array $post_data post 数据 * @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 的选项 * @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; } }
以上是“如何使用php实现微信自定义菜单创建/查询/取消功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。