<?php
$appid = "";//填写APPID
$appsecret = "";//填写AppSecret
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$output = https_request($url);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
$jsonmenu = '{
"button": [
{
"name": "开发测试",
"sub_button": [
{
"type": "click",
"name": "测试te",
"key": "绑定微信"
},
{
"type": "view",
"name": "绑定微信",
"url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx078e0e3c99caa895&redirect_uri=https://www.test.com/getopenid.php&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect" //这里通过微信端口获取code然后跳转到getopenid.php文件中在此文件中换取openid
}
]
}
]
}';
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$result = https_request($url, $jsonmenu);
//var_dump($result);
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;
}
?>
命名为mymenu.php
在网页中可以通过https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=ACCESS_TOKEN来查看返回的结果。
在网站中执行此文件使微信账号自定义菜单生效(有时候要等一段时间才会生效)。
<?php
$APPID='';//填写APPID
$REDIRECT_URI='https://www.test.com/getopenid.php';//获取openid的文件,首先通过https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.获取code(也就是此文件的作用),然后跳转到getopenid.php页面
//$scope='snsapi_base';
$scope='snsapi_userinfo';//需要授权
$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state=1#wechat_redirect';
header("Location:".$url);
?>
<?php
$code = $_GET["code"]; //从上个页面跳转到此页面会把code带过来
//往微信服务器发送请求的函数,主要为了获取openid
function getContentByUrl($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;
}
//获取AccessToken函数
function getAccessToken(){
$info = json2Arr(getContentByUrl("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".WEIXIN_APPID."&secret=".WEIXIN_APPSECRET));
return $info['access_token'];
}
//将json格式的数据转换为数组
function json2Arr($str){
return json_decode($str,true);
}
//获取openid
$info = json2Arr(getContentByUrl("https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$code."&grant_type=authorization_code"));
(将appid和secret替换为真实信息)
$openid= $info['openid'];//这里就会获取到openid
$_SESSION['openid']=$openid;//保存到session里面,其他页面可以通过session来使用
//也可以跳转到其他页面,不借用第三方绑定平台可以自己写个文件绑定例如:
header("Location:bind_account.php");
?>
此方法是通过oauth授权来实现,通过自定义菜单view按钮点击跳转到绑定页面。