授权获取个⼈信息头像姓名地区
<?php
namespace Home\Controller;
class GetWxUserController extends CommonController {
protected $appid = "wxe4a16ab6c280178c";
蚯蚓养殖技术protected $secret = "363cbcea8acb647f0b6ff8478d55c1d9";
/**
* 授权⼊⼝
* scope=snsapi_base/snsapi_userinfo
*/
public function index() {
$appid = 'wxe4a16ab6c280178c';
$loc_url = $_SERVER['HTTP_HOST'];
$redirect_uri = urlencode(''.$loc_url.'/GetWxUser/resultCode');
本机ip查询地址$url = "open.weixin.qq/connect/oauth2/authorize?
appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect"; header("Location:" . $url);
}
魔力宝贝2转/*
* 获取code信息
*/
public function resultCode() {
$code = I('de');
if (!empty($code)) {
return $code;
} else {
return NULL;
}
}
/**
* 获取⽤户信息
*/
public function getWXUser() {
$appid = $this->appid;
$secret = $this->secret;
$code = I('de');
//第⼀步:取全局access_token
$url = "api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret"; $token = $this->getJson($url);
//第⼆步:取得openid
怎样升级qq空间$oauth2Url = "api.weixin.qq/sns/oauth2/access_token?
appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = $this->getJson($oauth2Url);
//第三步:根据全局access_token和openid查询⽤户信息
$access_token = $token["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "api.weixin.qq/cgi-bin/user/info? access_token=$access_token&openid=$openid&lang=zh_CN"; $userinfo = $this->getJson($get_user_info_url);
//打印⽤户信息
// $userinfo = json_decode($userinfo, true);
if($userinfo['errcode'] == 40003){
$return = [
'status' => 40003,
'msg' => '获取失败',
'data' => $userinfo
];
} else {
$return = [
'status' => 200,
'msg' => '获取成功',
'data' => $userinfo
];
}
print_r($return);exit;
霸气名字return json_encode($return);
}
public function Login(){
$this->display();
}
private function getJson($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);台湾艺人kid
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
}