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

<?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文件:

<?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文件:

<?php
#define APPID1 "wxd1a93f73a10670a1"
#define APPSECRET1 "2d47501a563bade9d74601d36255d1e7"
?>

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

获取微信昵称、头像等信息方法相关推荐

  1. 微信公众号H5页面获取用户昵称头像等信息(Java)

    H5页面获取微信用户信息操作流程 (一)获取微信权限,由微信用户确认 (二)获取相应的ACCESS_TOKEN和OPENID信息 (三)根据ACCESS_TOKEN和OPENID信息获取相应的用户信息 ...

  2. 微信小程序如何获取微信昵称和头像

    <open-data type="userAvatarUrl"></open-data> 之前是使用open-data获取微信昵称和头像,但是更新之后,小程 ...

  3. 微信小程序【获取用户昵称头像和昵称(附源码)】

    获取用户昵称头像和昵称 wx.getUserProfile bindgetuserinfo 登录过程 前端源码:https://pan.baidu.com/s/1uz2Gm2FMB-8RFVkhlHY ...

  4. uniapp获取微信昵称和头像

    可以参考官方文档的示例 根据API调试进行获取 uni-app官网 (dcloud.net.cn) 多的不说 直接上示例看看我的代码 HTML部分 简单的搞了一个样式 样式就不用了多说了吧? < ...

  5. 微信小程序——获取微信昵称、头像、地区及性别弹框(官方自带)

    .wxml 文件 <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInf ...

  6. [533]python获取微信好友头像生成点阵图片

    首先是点阵字的概念:点阵字体是把每一个字符都分成 n * n 个点,然后用每个点的虚实来表示字符的轮廓.点阵字体也叫位图字体,其中每个字形都以一组二维像素信息表示. 汉字那么多,总不能每个字都去自己设 ...

  7. python获取微信好友头像拼接“2020”

    python获取微信好友头像拼接"2020" 背景 思路 源码说明 背景 2019年的最后一天,心想马上就要跨年,大家都在总结和祝福,但是又不想随大流,打开微信,翻看着通讯录,突然 ...

  8. 微信特殊字符php,PHP处理微信昵称特殊符号过滤方法

    我们在通过PHP获取微信昵称,并且存于数据库的时候,由于一些昵称带有特殊符号,所以存不进去,这时候我们可以通过下面的方式来处理. 方法二protected function removeEmoji($ ...

  9. 新浪云php与微信,新浪SAE php能够获取微信的头像,但是在本地运行代码获取不到?...

    在新浪云上建立了一个云应用SAE php标准环境,PHP获取微信的头像图片转换成base64字符串在网页上显示出来,在新浪云上代码正常执行,但是在本地用 WAMPSERVER 3.0.6 64bit启 ...

最新文章

  1. Power BI for Office 365(七) Power BI站点
  2. 关于敏捷开发和憋个大招两种开发方式的思考
  3. 「后端小伙伴来学前端了」记录自己的踩坑第一天 | CSS:vertical-align 属性
  4. keil_rtx特点
  5. java重新初始化吗_Java中为何已经重新赋值的变量在输出后会初始化?
  6. Ubuntu 13.10 用sogou拼音替换ibus-转
  7. Bulma - 基于 Flexbox 的现代化的 CSS 框架
  8. 《Linux内核设计与实现》读书笔记 第一章 Linux内核简介
  9. ThinkPHP视图模型实例
  10. 数据包络分析-超效率模型
  11. 道教门派封神大战九曲黄河迷仙大阵
  12. python 动态for循环的循环范围_Python中for循环的动态范围
  13. 勒让德Legendre变换
  14. [Android]天气App 1
  15. SAP AW01N 资产浏览器数据查看详解
  16. 【深度学习】BioBERT文章翻译及个人感悟
  17. 游戏模型外包-【精刚石数位】
  18. 2.1 良好的代码格式
  19. android 硬币翻转动画,使用Android标准动画显示正在翻转的硬币的两面
  20. (swing读书笔记)Swing Look And Feel(2)

热门文章

  1. 黑土地,你让我如此心醉
  2. MDX Aggregate函数
  3. 视频教程-EXCEL提升效率100招-Office/WPS
  4. 游戏开发人员眼中的Unity 3D网页游戏測评报告
  5. 微信公众账号与网站信息对接
  6. java telnet透传_透传模式DTU不能连接到TCP_Server
  7. 微信小程序学习第3天——网络数据请求
  8. conda\pip 安装pytorch
  9. python中scrapy可以爬取多少数据_python scrapy框架爬取某站博人传评论数据
  10. 倒计时|好礼知时节,Acala 糖果节参与热度持续升温!