29 lines
1.0 KiB
PHP
Executable File
29 lines
1.0 KiB
PHP
Executable File
<?php
|
|
$code = $_GET['code']; // 通过GET方式获取前端传递的code参数,code用户登录凭证(有效期五分钟)
|
|
|
|
|
|
$appid = 'wx3fbaf897825bf04f'; // 替换成你的小程序的AppID
|
|
$appsecret = '93b58209e014c9f151ac5f057b16ca33'; // 替换成你的小程序的AppSecret
|
|
|
|
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appsecret&js_code=$code&grant_type=authorization_code";
|
|
|
|
// 使用curl发送请求
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 不验证SSL证书,部署时建议验证
|
|
|
|
$output = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$result = json_decode($output, true);
|
|
|
|
// 获取openid
|
|
$openid = $result['openid'];
|
|
|
|
// 输出openid(你可以根据实际需求进行其他处理)
|
|
echo $openid;
|
|
|
|
// 或者直接将从微信服务器获取到的JSON数据返回给小程序
|
|
//echo $output;
|
|
?>
|