<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
class WechatController extends Controller
{
public function getOpenId(Request $request)
{
$code = $request->param('code');
$appid = 'your_wechat_app_id';
$secret = 'your_wechat_app_secret';
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
$result = file_get_contents($url);
$data = json_decode($result, true);
if (isset($data['openid'])) {
return json(['openid' => $data['openid']]);
} else {
return json(['error' => '无法获取 openid'], 400);
}
}
}
05-25
1198
04-22
446