<?php
header("content-type:text/html;charset=utf-8");
$corpid="xxxxx";//企业的CorpID
$corpsecret="xxxxxx";//企业的应用Secret
$url="http://www.xxxx.net/qywx.php";
$redirect_uri=urlencode($url);//授权后重定向的回调链接地址,请使用urlencode对链接进行处理
$response_type="code";//返回类型,此时固定为:code
//应用授权作用域。
//snsapi_base:静默授权,可获取成员的基础信息;
//snsapi_userinfo:静默授权,可获取成员的详细信息,但不包含手机、邮箱;
//snsapi_privateinfo:手动授权,可获取成员的详细信息,包含手机、邮箱。
$scope="snsapi_privateinfo";
$agentid="1000002";//微信企业应用的id
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$corpid."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$scope."&agentid=".$agentid."&state=STATE#wechat_redirect";
if (isset($_GET['code'])) {
$code=$_GET['code'];
//获取access_token
$filename=$agentid;
$access_token="";
if (file_exists($filename)) {
$tokenData=file_get_contents($filename);
$token=json_decode($tokenData,true);
if (time()>$token['expires_in']) {//已过期
$access_token="";
}else{
$access_token=$token['access_token'];
}
}
if (empty($access_token)) {
$url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.$corpid.'&corpsecret='.$corpsecret;
$tokenData=file_get_contents($url);
$token=json_decode($tokenData,true);
$access_token=$token['access_token'];
$token['expires_in']=time()+$token['expires_in'];
file_put_contents($filename, json_encode($token));
}
//获取用户信息
$url="https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code;
$userInfo=file_get_contents($url);
$user=json_decode($userInfo,true);
$UserId=$user['UserId'];
//读取成员具体信息
$url="https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token."&userid=".$UserId;
$userStr=file_get_contents($url);
$userArr=json_decode($userStr,true);
echo "<pre>";
var_dump($userArr);
}else{
header("Location:".$url);exit();
}
?>