微信公众号开发获取openid和用户信息的步骤

发布时间 2023-08-18 10:42:11作者: tochenwei
<?php

define("APPID", 'wxe147d9e245s9e343');
define("APP_SECRET", 'eb5ba07fee64we7uc5f0533cd11a6732');

header("Content-type: text/html; charset=utf-8");
session_start();

if (!isset($_GET['code'])) {
    // 回跳地址需要在微信公众号后台绑定,域名需要先备案
    $REDIRECT_URI = 'http://wx.myshop.com/wx_gzh_userinfo.php';
    $scope = 'snsapi_base'; //snsapi_base 或 snsapi_userinfo
    $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . APPID . '&redirect_uri=' . urlencode($REDIRECT_URI) . '&response_type=code&scope=' . $scope . '&state=wx' . '#wechat_redirect';
    header("Location:" . $url);
} else {

    $code = $_GET["code"];
    $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . APPID . '&secret=' . APP_SECRET . '&code=' . $code . '&grant_type=authorization_code';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $get_token_url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $res = curl_exec($ch);
    curl_close($ch);
    echo 'res=';
    print_r($res);
    $json_obj = json_decode($res, true);

    //根据openid和access_token查询用户信息,一般来说当openid在本网站数据库不存在的时候才需要这样做
    $access_token = $json_obj['access_token'];
    $openid = $json_obj['openid'];
    $get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $get_user_info_url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $res2 = curl_exec($ch);
    curl_close($ch);
    echo 'res2=';
    print_r($res2);
    //解析json
    $user_obj = json_decode($res2, true);
    $_SESSION['user'] = $user_obj;
    //print_r($user_obj);
    // print_r($user_obj);
    print_r("<br><br>");
    // print_r($get_user_info_url);
    // print_r("<br><br>");
    // print_r($openid);
    // return json_decode($openid);
    print_r($openid);
    // 最终结果
    /**
    res=
    {
        "access_token": "71_ilcxFIDf1zVpfOxABW_zEoJzxNO023S9UOQBfURQfZ5I6AgizJec8FQ5B86xJBb-1K135D1YfMyO6bgAmCp90OHGlYw_nuikqfulO9uIHxg",
        "expires_in": 7200,
        "refresh_token": "71_6HC0VxhT2LoOWUWUCc2-vG5z-6JZNR14ZULpMReqwwCpNOdRwdzvIGqpjFfbzpYtNl13t0QtIDG9onoLTiVI-a-SgFGoQbNhDFuYuBmA3dM",
        "openid": "oNglrtwW9aQQQJ4kLaIsqOPZm39g",
        "scope": "snsapi_userinfo",
        "unionid": "oXn8b1Advze2dIzcdY0_Efko25rs"
    }
    res2=
    {
        "openid": "oNglrtwW9aQQQJ4kLaIsqOPZm39g",
        "nickname": "闪电",
        "sex": 0,
        "language": "",
        "city": "",
        "province": "",
        "country": "",
        "headimgurl": "https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTJWVUzX3FnFxMcP8mqXrJZJ3bRG2OyauTr845kfR7q2bW1CLibiauuPAyV8QkqH8dyKP1l50ib2gyzvA\/132",
        "privilege": [],
        "unionid": "oXn8b1Advze2dIzcdY0_Efko25rs"
    }
    oNglrtwW9aQQQJ4kLaIsqOPZm39g
    */
}