微信服务号获取微信用户openid

suoden 幻丝 关注 五级站长 普通
发表于编程技术版块
新建文件 text.php

<?php


$appid = "wx0a75f2ebc13ufygi2c5";//修改为你的服务号开发者ID
$secret = "aeb255579544e561e29f2eytuytrhdf";//修改为你的服务号开发密码
$code = $_GET["code"];

$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);

// 获得 access_token 和openid
$access_token = $oauth2["access_token"];
$openid = $oauth2['openid'];

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);
}

$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);

var_dump($userinfo);//输出微信号昵称、头像、openid

再创建一个文件

go.php


$appid = 'wx0a75f2y7ioyg412c5';//你的服务号开发者id
$host='https://wap.xyz/text.php';//你的test.php访问地址
$redirect_uri = urlencode($host);
$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);


然后微信中打开链接  

https://localhost:443/go.php



评论列表 评论
发布评论

评论: 微信服务号获取微信用户openid

已有1次打赏
乐天打赏给楼主10天豆,2021-03-14 10:47:45
(5) 分享
分享

请保存二维码或复制链接进行分享

取消