欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

【PHP】一个微信公众号,多个域名下授权使用的方法!CODE中转

程序员文章站 2022-05-26 22:24:09
...
/*未授权微信域名下的方法!调用为了获取CODE*/

/**============================================================
* [get_cyb_code 从自己的主域名下获取CODE]
* @return [string] [code]
*/


public function get_cyb_code()
{
if(empty($_GET['code']))
{
$redirect_uri = urlencode($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
header("location:http://微信授权域名/wxcode.php?redirect_uri=".$redirect_uri);
}
else{
return $_GET['code'];
}
}

/*微信授权域名下的方法*/

$code = get_oauth2_code();//调用网页授权来获取code
echo $code;
if(!empty($code)){
header("location:http://".$_REQUEST['redirect_uri']."?code=".$code);
}
//echo $_REQUEST['redirect_uri'];


//微信授权域名下的,获取微信CODE方法

/**============================================================
* [get_oauth2_code 取得网页用户授权接口中code参数]
* @return [string] [code]
*/
function get_oauth2_code()
{
if(empty($_GET['code']))
{
if(!APPID)
{
$this->show_msg('appid error!');
return;
}
$redirect_uri = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
$para = array(
"appid" => APPID,
"redirect_uri" => $redirect_uri,
"response_type" => 'code',
"scope" => 'snsapi_base',
"state" => '123#wechat_redirect'
);
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".$para['redirect_uri']."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
header("location:".$url);
//echo $url;
}
else{
return $_GET['code'];
}
}


?>