if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {//判断是否为微信环境
if(isset($this->param['code']))//判断是否带有微信code码,没有的话则需要微信重定向
{
$code = $this->param['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->param['appid']}&secret={$this->param['secret']}&code=".$code."&grant_type=authorization_code";//跳转到授权页面,用户同意后获取用户信息
$data = json_decode(file_get_contents($url),1);
//取得授权后,拉取用户信息
if($data['scope'] == 'snsapi_userinfo'){
//先判断有没有关注公众号
$acurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->param['appid']}&secret={$this->param['secret']}";//获取access_token
$data1 = json_decode(file_get_contents($acurl),1);
$access_token = $data1['access_token'];
$gzurl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$data['openid']."&lang=zh_CN";//根据openid和token值获取关注信息
$gzdata = json_decode(file_get_contents($gzurl),1);
if($gzdata['subscribe'] == 1){//subscribe=1表示已关注
$userdata = $gzdata;
$isFans = 1;
}else{
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$data['access_token']."&openid=".$data['openid']."&lang=zh_CN";//未关注的话,则要根据openid和token值重新获取用户信息信息
$userdata = json_decode(file_get_contents($userurl),1);
$isFans = 0;
}
$openid = $userdata['openid'];
$nickname = $userdata['nickname'];
$picurl = $userdata['headimgurl'];
}else{//重定向
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->param['appid']}&redirect_uri=http://www.jyfair.com/jxs/firstact/index?jxs_id=".$jxs_id;
if(isset($param['aa'])){
$url .= "&aa=".$param['aa'];
}
$url .= "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
header("Location:".$url);exit;
}
}else{//重定向
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->param['appid']}&redirect_uri=http://www.jyfair.com/jxs/firstact/index?jxs_id=".$jxs_id;
if(isset($param['aa'])){
$url .= "&aa=".$param['aa'];
}
$url .= "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
header("Location:".$url);exit;
}
}else{//重定向
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6995571052457936&redirect_uri=http://www.jyfair.com/jxs/firstact/index?jxs_id=".$jxs_id;
if($param['aa']){
$url .= "&aa=".$param['aa'];
}
$url .= "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//print_r($url);
header("Location:".$url);exit;
}
PHP微信开发微信授权、拉取微信个人信息
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...