企业微信获取用户php,微信企业号获取用户授权登陆信息

今天做到一个企业号的授权登陆;与公众号授权登陆有稍微区别;

需要用到三个企业号的信息:

agentid:企业应用的id[企业号有 公众号无]

corpid:企业Id[类似公众好的appid]

corpsecret:管理组的凭证密钥[类似公众好的appsecret]

创建文件config.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

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 '';

}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 '';

}else{

echo "";

}

}

exit;

}else{//跳转进行code获取

$codeUrl = WX_CODE_URL;

$codeUrl = sprintf($codeUrl,WX_ID, urlencode(WEB_HOST),'gerinn');

header("Location:".$codeUrl);

exit;

}

}

?>配置好信用域名

参照官方文档一路下来也没有什么难点:

企业号文档

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值