微信企业号获取OpenID过程

 

define('CorpID',        "wx82e2c31215d9a5a7");
define('CorpSecret',	""); //当前管理组 设置->功能设置->权限管理->系统管理组->深圳管理(新建的管理组)->Secret
define('AgentID',		"20"); 	//当前应用ID

require_once("../../class/wxqiye.class.php");
$weixin = new class_wxqiye();
// var_dump($weixin);

if (isset($_COOKIE["openid"]) && !empty($_COOKIE["openid"])){
	$openid = $_COOKIE["openid"];
}else{
	if (!isset($_GET["code"])){
		$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_base", "123");
		// var_dump($jumpurl);
		Header("Location: $jumpurl");
	}else{
		$userinfo = $weixin->oauth2_get_userinfo($_GET["code"]);
        var_dump($userinfo);
		// array(2) { ["UserId"]=> string(6) "taojin" ["DeviceId"]=> string(32) "be434bab16db86bd40995edf1194ef3b" } 
		// array(2) { ["DeviceId"]=> string(32) "e0a66223f897a826717fc134930afe85" ["OpenId"]=> string(28) "o_UE0s2L8mVlEAhlScmE0dfjJojg" } 
		if (isset($userinfo["OpenId"])){
			$openid = $userinfo["OpenId"];
		}else{
			$openinfo = $weixin->convert_openid($userinfo["UserId"]);
            var_dump($openinfo);
			// array(4) { ["errcode"]=> int(0) ["errmsg"]=> string(2) "ok" ["openid"]=> string(28) "oxzslwktZytLTM7zwlU3QODJNzQ0" ["appid"]=> string(18) "wxcc23554f93463d7c" }
			$openid = $openinfo["openid"];
		}
        setcookie("openid", $openid, time()+86400); //一天后过期
	}
}

  

posted @ 2019-07-15 14:58 方倍工作室 阅读(...) 评论(...) 编辑 收藏