php微信公众号网页获取用户信息

本文转载:https://blog.csdn.net/qq_35713752/article/details/89854369

效果图:

index.php代码

<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{

    public function index()
    {
//        return '11111';die;
        header("Content-Type:text/html;charset=utf8");
        header("Access-Control-Allow-Origin: *"); //解决跨域
        header('Access-Control-Allow-Methods:POST');// 响应类型
        $appid = "wx3b0bc1d7ed723067";
        $appsecret = "282acce91946ca7338e61177e2f63c64";

        $redirect_uri = urlencode ( 'http://bg.jianhongkeji.cn/getuser');

        //第一步:取得code
        $codeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        function getJson($codeUrl){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $codeUrl);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            return json_decode($output, true);
        }
        print_r($codeUrl);
//        return '前台主页,暂无内容';
    }




}

userinfo.php代码

 

<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GetuserInfoController extends Controller
{

    public function index(){
        //header("Content-Type:text/html;charset=utf8");
        //header("Access-Control-Allow-Origin: *"); //解决跨域
        //header('Access-Control-Allow-Methods:POST');// 响应类型

        $appid = "wx3b0bc1d7ed723067";
        $appsecret = "282acce91946ca7338e61177e2f63c64";
        $code = $_GET["code"];

        //第一步:取得openid
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
        function getJson($url){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            return json_decode($output, true);
        }
        $oauth2 = getJson($oauth2Url);

        //第二步:根据全局access_token和openid查询用户信息
        $access_token = $oauth2["access_token"];
        $openid = $oauth2['openid'];
        //print_r($oauth2);
        $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
        $userinfo = getJson($get_user_info_url);
        dump($userinfo);die;
        //打印用户信息
        echo urldecode(json_encode($userinfo));

    }

}

参数    描述
openid    用户的唯一标识
nickname    用户昵称
sex    用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
province    用户个人资料填写的省份
city    普通用户个人资料填写的城市
country    国家,如中国为CN
headimgurl    用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
privilege    用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
unionid    只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。

————————————————
原文链接:https://blog.csdn.net/qq_35713752/article/details/89854369

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xingxingwuxin

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值