微信api 第三方平台授权 登录 ,获取用户信息

<?php
header('Content-type: text/html; charset=utf-8');#设置头信息
require_once('zhphpWeixinApi.class.php');#加载微信接口类文件
$zhwx=new zhphpWeixinApi();//实例化
$configArr=array(
				'token'=>'weixintest',
				'appid'=>'wx7b4b6ad5c7bfaae1',
				'appSecret'=>'faaa6a1e840fa40527934a293fabfbd1',
				'myweixinId'=>'gh_746ed5c6a58b'
				);
$zhwx->setConfig($configArr);//配置文件
if($zhwx->weixinBaseApiMessage()){
      $redirect_uri='http://d005151912.0502.dodi.cn/oauth.php';
      $url=$zhwx->getOauthorizeUrl($redirect_uri);//获取授权的url
      $content='<a href="'.$url.'">请点击授权</a>';
      $zhwx->responseMessage('text',$content);
}else{
		  echo '配置文件失败';
}

在手机微信上点击 链接,微信获取授权, 程序会跳转到 oauth.php


oauth.php

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 16-2-18
 * Time: 下午5:11
 * To change this template use File | Settings | File Templates.
 */
header('Content-type: text/html; charset=utf-8');#设置头信息
require_once('zhphpWeixinApi.class.php');#加载微信接口类文件
//如果用户确认授权之后,微信公众平台会返回code
$zhwx=new zhphpWeixinApi();//实例化
$configArr=array(
    'token'=>'weixintest',
    'appid'=>'wx7b4b6ad5c7bfaae1',
    'appSecret'=>'faaa6a1e840fa40527934a293fabfbd1',
    'myweixinId'=>'gh_746ed5c6a58b'
);
$zhwx->setConfig($configArr);//配置文件
if (isset($_GET['code'])){  //检查是否有 code 变量,授权后是有的
    if($zhwx->weixinBaseApiMessage()){//微信api 基本检查
         $info=$zhwx->getOauthAccessToken(); //通过code换取access_token与openid
         $userinfo=$zhwx->getOauthUserInfo($info['access_token'],$info['openid']);//传递access_token与openid 获取用户信息
         $content='';
         $content='openid:'.$userinfo['openid']."<br />";
         $content.='nickname:'.$userinfo['nickname']."<br />";
         $content.='sex:'.$userinfo['sex']."<br />";
         $content.='city:'.$userinfo['city']."<br />";
         $content.='province:'.$userinfo['province']."<br />";
         $content.='country:'.$userinfo['country']."<br />";
         echo '我们可以将这些信息写入数据库'.$content;

      }
    }else{
    echo "NO CODE";
}


发布了186 篇原创文章 · 获赞 24 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/echocdzh/article/details/50699777