開發(fā)之前噩斟,首選你需要已經(jīng)完成了基本的對(duì)接url通知等參數(shù)設(shè)置,拿到ticket?
然后根據(jù)官方的文檔
1.拿到第三方應(yīng)用的suite_access_token ,下面的代碼做了2小時(shí)的緩存庸队。
function getoken($suite_id,$suite_secret){
//suite_access_token 第三方應(yīng)用token
if(file_exists('atoken.json')){
$a=file_get_contents('atoken.json');
$b=json_decode($a,true);
if($b['expires_out']>time()){
return $b['token'];
}else{
}
}
$a=file_get_contents('ticket.txt');
$SuiteTicket=json_decode($a,true)['SuiteTicket'];
$url="https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token";
$json='{
"suite_id":"'.$suite_id.'" ,
"suite_secret": "'.$suite_secret.'",
"suite_ticket": "'.$SuiteTicket.'"
}';
$a=http_post_data($url,$json);
$b=json_decode($a,true);
if($b['errcode']==0){
$data['token']=$b['suite_access_token'];
$data['expires_out']=time()+7200;
file_put_contents('atoken.json',json_encode($data));
return $b['suite_access_token'];
}else{
echo $a;exit;
}
}
里面用到的suite_ticket,是微信每十分鐘推送過(guò)來(lái)的两芳,記得保存就行撵孤。
2.獲得預(yù)授權(quán)碼
function getpre_auth_code($suite_access_token){
//獲取 預(yù)授權(quán)碼
$url="https://qyapi.weixin.qq.com/cgi-bin/service/get_pre_auth_code?suite_access_token=".$suite_access_token;
$a=file_get_contents($url);
$b=json_decode($a,true);
return $b['pre_auth_code'];
}
3.獲得臨時(shí)授權(quán)碼?
? 注意:臨時(shí)授權(quán)碼是在第一次安裝應(yīng)用時(shí)迈着,官方推送給你的,所以你要截取到邪码,下面是截取的部分代碼
$postObj = simplexml_load_string($sMsg, 'SimpleXMLElement', LIBXML_NOCDATA);
? ? ? ? $InfoType = trim($postObj->InfoType);
//獲取ticket
if($InfoType=='suite_ticket'){
file_put_contents('ticket.txt', json_encode($postObj));
}
//獲取臨時(shí)授權(quán)碼
if($InfoType=='create_auth'){
$SuiteId = trim($postObj->SuiteId);
file_put_contents('tempuser/'.$SuiteId.'_auth.txt', json_encode($postObj));
}
獲取到的是xml,我把它保存成了json:
{"SuiteId":"ww2b95e5baf***","AuthCode":"jju6cegS3U5wl3T6aak27G_eioFJ3xMl0wzeevQxgKObkTZanmw1Wi6r9PwaFKkgEbUiLFRGjwfkGreWcnd5q1IpPEyiDfxVb6Q1U-meEnc","InfoType":"create_auth","TimeStamp":"1631423859"}
4.根據(jù)臨時(shí)授權(quán)碼-獲得永久授權(quán)碼?
function get_permanent_code($suite_access_token,$auth_code){
? // 獲取永久授權(quán)碼
$url="https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code?suite_access_token=".$suite_access_token;
$json='{
? ? "auth_code": "'.$auth_code.'"
}';
$a=http_post_data($url,$json);
$b=json_decode($a,true);
return $b;
}
5.根據(jù)永久授權(quán)碼裕菠,獲得企業(yè)授權(quán)信息?
function get_auth_info($auth_corpid,$permanent_code,$suite_access_token){
? // 獲取永久授權(quán)碼
$url="https://qyapi.weixin.qq.com/cgi-bin/service/get_auth_info?suite_access_token=".$suite_access_token;
$arr=array('auth_corpid'=>$auth_corpid,'permanent_code'=>$permanent_code);
$json= json_encode($arr);
$a=http_post_data($url,$json);
$b=json_decode($a,true);
return $b;
}
好了,獲得了企業(yè)信息后霞扬,你就可以錄入你的數(shù)據(jù)庫(kù)了糕韧。