微信自动登录获取openid
微信小程序自动登录之后会获取到一个code。
把code传入以下php代码。可以直接获取openid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Demo { protected $appid = 'your_appid'; protected $appsecret = 'your_appsecret'; public function getOpenidByCode($code) { $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $this->appid . "&secret=" . $this->appsecret . "&js_code=" . $code . "&grant_type=authorization_code"; $weixin = file_get_contents($url); $jsondecode = json_decode($weixin, true); $openid = $jsondecode['openid']; return $openid; } }
|
Author: Linyin
Permalink: https://linyin022.github.io/posts/2712931618.html
License: Copyright (c) 2019 CC-BY-NC-4.0 LICENSE