zoukankan      html  css  js  c++  java
  • PHP微信登錄(網頁授權)之後的獲取用戶的信息

    //這部峯代碼是封裝的庫文件,
    <?php /** * Created by PhpStorm. * User: root * Date: 16-6-23 * Time: 下午3:29 */ class weixinUser{ protected $m_appid;//应用唯一标识,在微信开放平台提交应用审核通过后获得 protected $m_AppSecret;//应用密钥AppSecret,在微信开放平台提交应用审核通过后获得 function __construct($appid, $m_AppSecret){ //初始化 $this->m_appid = $appid; $this->m_AppSecret = $m_AppSecret; } //根據用戶授權登錄之後渠道的code得到access_token public function get_access_token($code) { $access_token_url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->m_appid."&secret=".$this->m_AppSecret."&code=".$code."&grant_type=authorization_code"; $access_token = json_decode(file_get_contents($access_token_url)); if (isset($access_token->errcode)) { $this->error($access_token->errcode, $access_token->errmsg); return 0; } else { return $access_token; } } //根據accesss_token取到用戶的個人信息 public function get_user_info($access_token, $language="zh_CN") { $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token->{'access_token'}."&openid=".$access_token->{"openid"}; $user_info = json_decode(file_get_contents($url)); if (isset($user_info->errcode)) { $this->error($user_info->errcode,$user_info->errmsg); return 0; }else{ return $user_info; } } //輸出錯誤信息 public function error($errmsg, $errcode = "001") { echo '<h1>error:</h1>' . $errcode; echo '<br/><h2>error information:</h2>' . $errmsg; } }

    微信登錄(網頁授權)之後的獲取用戶的信息需要三步:

    第一步:詳見網頁微信授權(我的博客微信開發分類中);

    第二步:獲取access_token;

    第三步:獲取用戶的個人信息;

    下面是對上面函數的調用:

    <?php
    include_once "WeixinUser.php";
    $code = $_GET['code'];
    //换成自己的接口信息, 這兩個信息都是公衆號的信息,不是開放平臺的信息。
    $appid = 'wxcd3024b539782879';  
    $AppSecret='06e3cfbccb239d029dee5f6cb9c1dbb4';
    $weixinUser=new weixinUser($appid,$AppSecret); //初始化對象

    $access_token=$weixinUser->get_access_token($code);
    var_dump($access_token); //打印access_token,注意:打印的是一個對象,而不是數組。調用其成員變量時要用"->"
    $user=$weixinUser->get_user_info($access_token);
    var_dump($user); //打印用戶的個人信息 ?>

    至此,用戶的信息就獲取到了。接下來做公衆號的php支付了。詳見博客微信開發 ---  微信支付PHP SDK —— 公众号支付代码详解。

    2016-06-23  21:09:31

  • 相关阅读:
    centos8 安装vmware需要的内核头文件 kernel-headers.
    centos7开启ssh服务
    systemctl命令的使用及服务状态的查看
    centos WPS 字体安装
    CentOS8 使用 aliyun 阿里云 镜像站点的方法
    CentOS提示::unknown filesystem type 'ntfs'自动挂载NTFS分区的U盘或者移动硬盘
    Aria2 Centos8 安装配置
    centos7 更新Firefox版本
    线程内容详解
    进程池、进程池和多进程的性能测试、进程池的其他机制、进程池的回调函数
  • 原文地址:https://www.cnblogs.com/mafeng/p/5612265.html
Copyright © 2011-2022 走看看