PHP实现微信公众号企业号自定义菜单接口示例
程序员文章站
2024-03-11 10:54:01
本文实例讲述了php实现微信公众号企业号自定义菜单接口。分享给大家供大家参考,具体如下:
define(appid, "wx666cae44xxxxxx2");/...
本文实例讲述了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 = '{//构造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" } ] } ] }'; $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 ('http' => array ('method' => "post", 'header' => "user-agent: mozilla/5.0 (windows; u; windows nt 6.0; en-us) \r\n accept: */*", 'content' => $post_string ) ); $stream_context = stream_context_create ( $context ); $data = file_get_contents ( $url, false, $stream_context ); return $data; } ?>
更多关于php相关内容感兴趣的读者可查看本站专题:《php微信开发技巧汇总》、《php编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php字符串(string)用法总结》、《php中json格式数据操作技巧汇总》及《php针对xml文件操作技巧总结》
希望本文所述对大家php程序设计有所帮助。