• 欢迎光临~

php获取微信小程序用户openid

开发技术 开发技术 2022-07-26 次浏览
<?php

//http请求
function httpRequest($url, $data = null) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}

$appid = "";
$appsecret = "";
//小程序中获取
$js_code = "";

$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $appsecret . "&js_code=" . $js_code . "&grant_type=authorization_code";
$res = httpRequest($url);
$wx_res = json_decode($res,true);

//打印错误
if(!empty($wx_res['errcode'])) echo $wx_res['errcode'].':'.$wx_res['errmsg'];

//openid获取成功
echo $wx_res['openid'];

?>

搜索

复制

程序员灯塔
转载请注明原文链接:php获取微信小程序用户openid
喜欢 (0)