博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php取微信名字和头像,获取微信昵称、头像等信息方法
阅读量:4318 次
发布时间:2019-06-06

本文共 4237 字,大约阅读时间需要 14 分钟。

在PHP文件里加入以下代码:

require_once(getUserInfo/getUserInfo.php); //获取微信信息php

$test = new Webweixin();

$userinfo = $test->get_userinfo();

print_r(headimgurl:.$userinfo['headimgurl'].openid:.$userinfo['openid'].nickname:.$userinfo['nickname'].city:.$userinfo['city'].province:.$userinfo['province'].country:.$userinfo['country'].sex:.$userinfo['sex']);

?>

getgetUserInfo.php文件:

require_once(weixin.config.php);

class Webweixin

{

//APPID 默认是服务号

var $APPID = APPID1;

var $APPSECRET = APPSECRET1;

//用户方信息(存储当前交互用户的操作状态,以及状态时效)

var $_client = array('wx_id'=>'', 'user_id'=>0, 'act'=>'', 'exp'=>0, 'token'=>'','userdata'=>'');

var $wxu_mod;

var $CODE = '';

//var $_userinfo;

public function get_userinfo()

{

$this->APPID = 'wxd1a93f73a10670a1';

$this->APPSECRET = '2d47501a563bade9d74601d36255d1e7';

if (isset($_GET['code']))

{

$this->CODE = $_GET['code'];

$userinfo = $this->getUserInfo();

//$_userinfo = $userinfo;

//$json_data = json_decode($response);

//print_r($userinfo);

//.city:.$json_data->{'city'}.province:.$json_data->{'province'}

}

return $userinfo;

}

function __construct()

{

}

public function getUserInfo()

{

$accessToken = $this->getAccessToken();

$cfg['ssl'] = true;

//https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID

$userinfo = $this->__curlOpen(https://api.weixin.qq.com/sns/userinfo?access_token=.$accessToken.&openid=.$this->_client['wx_id'].'&lang=zh_CN', $cfg);

$userinfo = json_decode($userinfo,true);

return $userinfo;

}

/**

* 获取ACCESS TOKEN

*/

public function getAccessToken($getHTTP = false)

{

$isCurl = true;

/*

$tokenfile = ROOT_PATH . /temp/TOKEN_WEB;

$token = file_exists($tokenfile)?file_get_contents($tokenfile):'';

if($token)

{

$token = json_decode($token,true);

if( time()-$token['access_time'] < $token['expires_in']){

$isCurl = false;

}

}*/

if($isCurl || $getHTTP)

{

//https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

$cfg['ssl'] = true;

$token = $this->__curlOpen(https://api.weixin.qq.com/sns/oauth2/access_token?appid=.$this->APPID.&secret=.$this->APPSECRET.&code=.$this->CODE.&grant_type=authorization_code, $cfg);

$token = json_decode($token,true);

$token['access_time'] = time();

//file_put_contents($tokenfile, json_encode($token), LOCK_EX);

}

$this->_client['wx_id'] = $token['openid'];

//$client = $this->wx_user();

if($client){

$this->_client = $client;

}else{

//$this->wx_user('add');

}

return $token['access_token'];

}

public function __curlOpen($url, $cfg)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

//isset($cfg['post']) && curl_setopt($ch, CURLOPT_CUSTOMREQUEST, POST);

//if($cfg['ssl'])

//{

//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

//}

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_AUTOREFERER, 1);

//isset($cfg['post']) && curl_setopt($ch, CURLOPT_POSTFIELDS, $cfg['post']);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

//echo qqqqqqqqqqqqqq;

$result = curl_exec($ch);

if (curl_errno($ch))

{

//echo wwwwwwwwwww;

return curl_error($ch);

}

//echo bbbbbbbbb;

curl_close($ch);

return $result;

}

//微信用户信息操作

public function wx_user($act='')

{

if($this->_client['wx_id'])

{

$this->wxu_mod = &m(weixinuser);

if($act=='add')

{

$this->wxu_mod->add($this->_client);

}elseif($act=='edit'){

$this->wxu_mod->edit(wx_id='.$this->_client['wx_id'].', $this->_client);

}else{

$client = $this->wxu_mod->get(wx_id='.$this->_client['wx_id'].');

return $client;

}

}

}

/*$scope : snsapi_base / snsapi_userinfo*/

public function makeStartUrl($url, $state = '', $scope = 'snsapi_userinfo')

{

//https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

$base_url = https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->APPID}&redirect_uri=;

//$base_url .= rawurlencode($url);

$base_url .= $url;

$base_url .= &response_type=code&scope={$scope}&state={$state}#wechat_redirect;

return $base_url;

}

}

?>

weixin.config.php文件:

#define APPID1 wxd1a93f73a10670a1

#define APPSECRET1 2d47501a563bade9d74601d36255d1e7

?>

通过以上即可实现获取微信用户信息。

转载地址:http://awgzs.baihongyu.com/

你可能感兴趣的文章
POJ 2584 T-Shirt Gumbo
查看>>
搭建Springboot
查看>>
Js触发ASP.NET Validation控件的验证, 同时获取前台验证结果(不包括CustomValidator)
查看>>
react初始化阶段
查看>>
【OpenStack】OpenStack系列12之OpenStack自动化测试详解
查看>>
static总结
查看>>
【公众号】微信第三方登录(静默授权和非静默授权)(具体代码:U盘 新浪云SAE)...
查看>>
『转』一千行MySQL学习笔记
查看>>
CLR_Via_C#学习笔记之事件
查看>>
Windows Installer (MSI)知识学习
查看>>
kafka producer 序列化& 反序列化 & 分区分配计算
查看>>
用DecimalFormat格式化十进制数字的实际应用
查看>>
身份证照片比对,身份鉴别,人脸实名
查看>>
jupyter环境安装
查看>>
cordova添加platform
查看>>
AngularJS $http配置为form data 提交
查看>>
闭包2
查看>>
轮播图组件及vue-awesome-swiper的引入
查看>>
Java 笔记07
查看>>
POJ 3041 Asteroids (二分匹配)
查看>>