文章目录

微信公众号自定义菜单接口开发(PHP)

首先获取access_token,如下代码:

php代码function getAccessToken(){
	$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
}

getCurl函数代码如下:

php代码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;
}

以下是https_request函数,代码如下:

php代码function https_request($url,$data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

自定义菜单:

php代码//自定义菜单开始
$jsonmenu = '{
"button":[
	{
	"name":"服务端",
		"sub_button":[
			{
				"type":"view",
				"name":"php",
				"url":"http://www.santii.com/php"
			},
			{
				"type":"view",
				"name":"python",
				"url":"http://www.santii.com/python"
			},
			{
				"type":"view",
				"name":"java",
				"url":"http://www.santii.com/java"
			}
		]
	}
	,{
	"name":"移动端开发",
		"sub_button":[
			{
				"type":"view",
				"name":"微信支付",
				"url":"http://www.santii.com/weixinpay"
			},
			{
				"type":"view",
				"name":"微信公众号",
				"url":"http://www.santii.com/wxmp"
			},
			{
				"type":"view",
				"name":"微信小程序",
				"url":"http://www.santii.com/wxxcx"
			}
		]
	}
	,
	{
		"type":"view",
		"name":"在线教程",
		"url":"http://www.santii.com"
	}
]
}';
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".getAccessToken();
$result = https_request($url, $jsonmenu);
var_dump($result);
//自定义菜单结束

本文网址:http://www.santii.com/article/244.html

您可能还会对这些微信公众号文章感兴趣

数据更新时间:2020-07-14