PHP实现微信公众号企业号自定义菜单接口

PHP实现微信公众号企业号自定义菜单接口

熊掌号:webit爱好者

头条号:webit爱好者

企鹅号:webit爱好者

更新内容:小编的个人建站心得,并分享最新的IT科技资讯。

唯一官网:www.web-it.cn

文章来源:脚本之家

链接直达:www.jb51.net/article/121706.htm

欢迎您的关注!

define(AppId, "xxx");

//定义AppId

define(AppSecret,"xxx");

//定义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.ss.com/fenxiao/jianjie/soft.html"

},

{

"type":"view",

"name":"分销B型",

"url":"http://www.ss.com/fenxiaob/jianjie/soft.html"

},{

"type":"view",

"name":"地接批发",

"url":"http://www.ss.com/dijie/jianjie/soft.html"

},{

"type":"view",

"name":"精简组团",

"url":"http://www.ss.com/zutuan/jianjie/soft.html"

},{

"type":"view",

"name":"直客网站",

"url":"http://www.ss.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;

}

?>

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181129A1NB8C00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码关注云+社区

领取腾讯云代金券