0, 'msg' => '缺少微信临时凭证']); exit; } // 【重要】:请替换为您小程序的真实 AppID 和 AppSecret // 可以在微信公众平台 -> 开发管理 -> 开发设置中找到 $appid = 'wx290db68c13e08756'; $secret = 'ea1ceb6a660c6432b6a0148912f218f3'; // 服务器代替小程序去向微信索要 OpenID $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code"; $response = file_get_contents($url); $result = json_decode($response, true); if (isset($result['openid'])) { // 成功拿到唯一 ID,返回给小程序 echo json_encode(['code' => 1, 'msg' => '成功', 'data' => ['openid' => $result['openid']]]); } else { echo json_encode(['code' => 0, 'msg' => '获取失败', 'data' => $result]); } exit; } echo json_encode(['code' => 0, 'msg' => '接口不存在']); ?>