今天做到一個企業号的授權登陸;與公衆号授權登陸有稍微差別;
需要用到三個企業号的資訊:
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" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" </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" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" </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;
}
}
?>
配置好信用域名
參照官方文檔一路下來也沒有什麼難點: 微信企業應用開發文檔