<?php class Weixin_menu extends My_Controller { public function __construct() { parent::__construct(); } public function menu(){ $appid = "wxe0745a5de6f0c3e6"; $appsecret = "f21999aa967bc522e2d542c2ff0b5e5e"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $output = $this->https_request($url); $jsoninfo = json_decode($output, true); $access_token = $jsoninfo["access_token"]; $jsonmenu = '{ "button": [ { "name": "逍游中心", "sub_button": [ { "type": "click", "name": "1", "key": "1" }, { "type": "click", "name": "2", "key": "2" }, { "type": "click", "name": "3", "key": "3" }, { "type": "click", "name": "4", "key": "4" }, { "type": "view", "name": "本地天气", "url": "http://m.hao123.com/a/tianqi" } ] }, { "name": "逍游手游", "sub_button": [ { "type": "click", "name": "10", "key": "10" }, { "type": "click", "name": "20", "key": "20" }, { "type": "click", "name": "30", "key": "30" }, { "type": "click", "name": "40", "key": "40" } ] }, { "name": "逍游天下", "sub_button": [ { "type": "click", "name": "公司简介", "key": "company" }, { "type": "click", "name": "趣味游戏", "key": "game" }, { "type": "click", "name": "领取兑换码", "key": "dhm" } ] } ] }'; $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token; $result = $this->https_request($url, $jsonmenu); //var_dump($result); } public function https_request($url,$data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } }