PHP 微信网页授权开发


现在有大量的app分享 、点进去看视频都微信授权登录这一说,不过今天抽空把微信授权做了一下,也耗了点时间、授权回调页面域名 这个地方刚开始做的时候也没有显示正确、redirect_url  参数错误,也就是你访问你项目的地址、大家一定不要填错了

第一步:百度搜索 微信公众平台 --> 登录、进去完之后、往下拉看到微信开发者工具-》公众平台测试账号。






///改你自己本地的域名

扫描二维码关注公众号,回复: 2669723 查看本文章
public function getOpendId(){
    $appid= "wx2e94e0f2c20f6144";
    $redirect_uri  = urlEncode('http://192.168.2.104/api.php/Register/getUserInfo'); 
    $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
    Header("Location: $url");
}
//获取用户信息
public function getUserInfo(){
    $code = $_GET['code'];
    $appid= "wx2e94e0f2c20f6144";
    $appsecret = "aec1349c37c1f8580525bc8c092fe491";
    //获取access_token
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
    $WxCon = file_get_contents($url);
    $WxCon = json_decode($WxCon,true);
    $access_token = $WxCon['access_token'];
	//获取用户信息
    $userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$appid&lang=zh_CN";
    $userInfo = file_get_contents($userInfoUrl);
    echo '<pre>';
    $userArr = json_decode($userInfo,true);
    print_r($userArr);
}

  效果:


//然后对此进行一些处理 等- - 、

小伙伴们多看一下文档:

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

猜你喜欢

转载自blog.csdn.net/hua950327/article/details/78183879