首先你的公衆号必須是企業号
<?php
if(isset($_SESSION['user'])){
print_r($_SESSION['user']);//判斷是否已經登入
exit;
}
$APPID='你的APPID';
$REDIRECT_URI='授權登陸後需要跳轉的界面';//url中的特殊字元如&、/等需要轉碼
$scope='snsapi_userinfo';//需要授權
$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.$REDIRECT_URI.'&response_type=code&scope='.$scope.'&state=1'.$state.'#wechat_redirect';
header("Location:".$url);
?>
這個是跳轉進來的php
<?php
$appid = "<span style="font-family: Arial, Helvetica, sans-serif;">你的APPID</span><span style="font-family: Arial, Helvetica, sans-serif;">"; </span>
$secret = "你的SECRET";
$code = $_GET["code"];
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';//擷取token的url
$access_token = json_decode(file_get_contents($get_token_url));
$access_tokens = $access_token->access_token;
$openid = $access_token->openid;
$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_tokens.'&openid='.$openid.'&; //擷取使用者資訊的url
$user_info = json_decode(file_get_contents($get_user_info_url));
$_SESSION['user'] = $user_info;
echo $user_info->nickname;//得到使用者資訊
echo $user_info->headimgurl;
?>