【干货】微信开发篇之菜单栏(access_token的获取)(三)!

【思路】:利用appid和appsecret请求接口拿到access_token
然后再利用Access_token再次请求接口即可
【获取access的url】

$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$AppID."&secret=".$Appsecret;

【利用access请求接口的url】

     $curl="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;

【注意:我是面向过程所以请求接口的代码写了两遍
面向对象时封装成方法即可“食”用】
下面是完整菜单代码

function createMenu(){
$AppID = '你的appid';
$Appsecret = '你的appsecret;
$data = '{
    "button":[
    {
      "type":"click",
      "name":"首页",
      "key":"音乐"
    },
    {
       "type":"view",
       "name":"慕白博客",
       "url":"http://www.php1314.cn/"
    },
    {
       "name":"菜单",
       "sub_button":[
       {
         "type":"click",
         "name":"hello word",
         "key":"V1001_HELLO_WORLD"
       },
       {
         "type":"click",
         "name":"赞一下我们",
         "key":"V1001_GOOD"
       }]
     }]
 }';

$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);

    if (!empty($data)){
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
$access = json_decode($output,true);
  $access_token = $access['access_token'];
    //var_dump($access['access_token']);
     $curl="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
     $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $curl);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    if (!empty($data)){
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $request = json_decode($output,true);
    var_dump($request);
}
createMenu();

为您推荐

评论已关闭