实现微信公众号企业号中自定义菜单接口的php代码

黄舟
发布: 2023-03-15 13:50:02
原创
2230 人浏览过

这篇文章主要介绍了PHP实现微信公众号企业号自定义菜单接口,结合实例形式分析了php自定义微信菜单类及微信接口相关操作技巧,需要的朋友可以参考下

本文实例讲述了PHP实现微信公众号企业号自定义菜单接口。分享给大家供大家参考,具体如下:


define(AppId, "wx666cae44xxxxxx2");//定义AppId,需要在微信公众平台申请自定义菜单后会得到
define(AppSecret, "d77026a714d443a01d0229xxxxxxxx");//定义AppSecret,需要在微信公众平台申请自定义菜单后会得到
include("menu.php");//引入微信类
$wechatObj = new Wechat();//实例化微信类
$creatMenu = $wechatObj->creatMenu();//创建菜单
登录后复制

微信类(menu.php)代码


<?
class Wechat
{
 private function getAccessToken() //获取access_token
 {
 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppId."&secret=".AppSecret;
 $data = getCurl($url);//通过自定义函数getCurl得到https的内容
 $resultArr = json_decode($data, true);//转为数组
 return $resultArr["access_token"];//获取access_token
 }
 public function creatMenu()//创建菜单
 {
 $accessToken = $this->getAccessToken();//获取access_token
 $menuPostString = &#39;{//构造POST给微信服务器的菜单结构体
 "button":[
   {
      "name":"产品介绍",
      "sub_button":[
      {
        "type":"view",
        "name":"分销A型",
        "url":"http://www.yourwebname.com/fenxiao/jianjie/soft.html"
      },
      {
        "type":"view",
        "name":"分销B型",
        "url":"http://www.yourwebname.com/fenxiaob/jianjie/soft.html"
      },{
        "type":"view",
        "name":"地接批发",
        "url":"http://www.yourwebname.com/dijie/jianjie/soft.html"
      },{
        "type":"view",
        "name":"精简组团",
        "url":"http://www.yourwebname.com/zutuan/jianjie/soft.html"
      },{
        "type":"view",
        "name":"直客网站",
        "url":"http://www.yourwebname.com/tripal/jianjie/soft.html"
      }]
    },
   {
      "name":"申请试用",
      "sub_button":[
      {
        "type":"click",
        "name":"分销A型",
        "key":"fxa"
      },
      {
        "type":"click",
        "name":"分销B型",
        "key":"fxb"
      },
      {
        "type":"click",
        "name":"地接批发",
        "key":"dj"
      },
      {
        "type":"click",
        "name":"精简组团",
        "key":"zutuan"
      },
      {
        "type":"click",
        "name":"直客网站",
        "key":"zhike"
      }
      ]
    },
      {
      "name":"博纵在线",
      "sub_button":[
      {
        "type":"view",
        "name":"企业介绍",
        "url":"http://www.yourwebname.com/about.html"
      },
      {
        "type":"view",
        "name":"公司新闻",
        "url":"http://www.yourwebname.com/news/company/"
      },
      {
        "type":"view",
        "name":"联系我们",
        "url":"http://www.yourwebname.com/contact.html"
      }
      ]
    }
    ]
 }&#39;;
 $menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accessToken;//POST的url
 $menu = dataPost($menuPostString, $menuPostUrl);//将菜单结构体POST给微信服务器
 }
}
function getCurl($url){//get https的内容
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 $result = curl_exec($ch);
 curl_close ($ch);
 return $result;
}
function dataPost($post_string, $url) {//POST方式提交数据
 $context = array (&#39;http&#39; => array (&#39;method&#39; => "POST", &#39;header&#39; => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*", &#39;content&#39; => $post_string ) );
 $stream_context = stream_context_create ( $context );
 $data = file_get_contents ( $url, FALSE, $stream_context );
 return $data;
}
?>
登录后复制

以上是实现微信公众号企业号中自定义菜单接口的php代码的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!