标签:
namespace Home\Controller;
use Think\Controller;
define(‘appid‘,‘wx33584f71b4a84fa9‘);
define(‘appsecret‘,‘‘);
class MenuController extends Controller
{
private function access_token(){
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".appid."&secret=".appsecret."";
$ch=curl_init();//初始化
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl_exec($ch);
curl_close($ch);
$jsoninfo=json_decode($output,true);
$access_token=$jsoninfo[‘access_token‘];
print_r($access_token.‘\n‘);
return $access_token;
}
function set_menu(){
//菜单
$data = ‘ {
"button":[
{
"name":"PHP100",
"sub_button":[
{
"type":"click",
"name":"互联网热点",
"key":"A1"
},
{
"type":"click",
"name":"悬赏与外包",
"key":"A2"
},
{
"type":"click",
"name":"个人中心",
"key":"A5"
}]
},
{
"name":"编程助手",
"sub_button":[
{
"type":"click",
"name":"函数查询",
"key":"B2"
},
{
"type":"click",
"name":"热门书籍",
"key":"B3"
},
{
"type":"click",
"name":"教程与讲座",
"key":"B4"
},
{
"type":"click",
"name":"培训与课程",
"key":"B5"
}]
},
{
"name":"自娱自乐",
"sub_button":[
{
"type":"click",
"name":"发布心情",
"key":"C1"
},
{
"type":"click",
"name":"开心段子",
"key":"C2"
},
{
"type":"click",
"name":"免费与优惠",
"key":"C3"
}]
}]
}‘;
//make post url
$url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$this->access_token()}";
$ch=curl_init();//初始化
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_exec($ch);
curl_close($ch);
print_r($url);
}
}
标签:
原文地址:http://www.cnblogs.com/llforeverlove/p/5263876.html