今天做到一个企业号的授权登陆;与公众号授权登陆有稍微区别;
需要用到三个企业号的信息:
agentid:企业应用的id[企业号有 公众号无]
corpid:企业Id[类似公众好的appid]
corpsecret:管理组的凭证密钥[类似公众好的appsecret]
创建文件config.php 里面包含一些常量与自定义函数
<?php
define('WX_ID','wx011c30b1a3e2AAAA');
define('WX_SC','bFgmCcaoxF7stnIUXStM3LloMvZUiNieTzd3EVcBBBB');
define('WX_CODE_URL','https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&agentid=1000002&state=%s#wechat_redirect');
define('WX_TOKEN_URL','https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=%s&code=%s');
define('WX_USER_URL','https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token=%s');
define('WX_ACCESS_TOKEN','https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s');
define('WEB_HOST','http://www.a.com/abc');
function getAccessToken($corpid,$secrect) {
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
$url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".$corpid."&corpsecret=".$secrect;
$res = json_decode(httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 6000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
function httpPost($url,$data_string){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
return $result;
}
?>
创建入口文件index.php
<?php
require_once('./config.php');
$wxid = isset($_COOKIE['wxida'])?$_COOKIE['wxida']:false;
if(!$wxid){
if(isset($_GET['code'])){//获取到code
$token = getAccessToken(WX_ID,WX_SC);//获取toekn
$tokenUrl = sprintf(WX_TOKEN_URL,$token,$_GET['code']);//组装URL获取user_ticket
$tokenStr = file_get_contents($tokenUrl);
$tokenArr = json_decode($tokenStr,true);
if(isset($tokenArr['user_ticket'])){
$userUrl = sprintf(WX_USER_URL,$token);//组装URL获取用户信息
$post['user_ticket'] = $tokenArr['user_ticket'];
$userStr = httpPost($userUrl, json_encode($post));
$userArr = json_decode($userStr,true);
if($userArr['errcode']>0){
echo '<script>alert("'.$tokenArr['errmsg'].'");window.location.href="./index.php"</script>';
}else{
$wxid = $userArr['userid'];
$wxname = $userArr['name'];
$wximg = $userArr['avatar'];
setcookie('wxida',$wxid,time()+86400*30);
$wxnameB = base64_encode($wxname);
$sql = 'insert into weishang_users (id,`wxid`,`wximg`,`wxname`,`creates`) Values(NULL,"'.$wxid.'","'.$wximg.'","'.$wxnameB.'",NOW()) on duplicate key update `wxname`="'.$wxnameB.'"';
mysql_query($sql);
header("Location:".WEB_HOST);
}
}else{//没有获取user_ticket两种情况 1出错2非成员
if($tokenArr['errcode']>0){
echo '<script>alert("'.$tokenArr['errmsg'].'");window.location.href="./index.php"</script>';
}else{
echo "<script>alert('你非企业号成员');WeixinJSBridge.call('closeWindow');</script>";
}
}
exit;
}else{//跳转进行code获取
$codeUrl = WX_CODE_URL;
$codeUrl = sprintf($codeUrl,WX_ID, urlencode(WEB_HOST),'gerinn');
header("Location:".$codeUrl);
exit;
}
}
?>
配置好信用域名
参照官方文档一路下来也没有什么难点: 微信企业应用开发文档