java微信公众号怎么获取openid(微信公众号联合登录\u0026微信

生活常识 2023-05-15 15:36生活常识www.xinxueguanw.cn

微信公众号平台登陆入口 微信公众号联合登录微信小程序获取openid头条获取openid

#PHP##小程序##微信小程序##抖音##抖音小程序#

/ 微信公众号联合登录 @return array|int @throws DataNotFoundException @throws DbException @throws LogicException @throws ModelNotFoundException /public function openid(): array{ $code = $this-gtrequest-gtparam(#39code#39, #39#39) if (empty($code)) throw new LogicException(#34code必传#34) $secret = Config::get(#39business.wechat.secret#39) $appid = Config::get(#39business.wechat.appid#39) $url = #34https://api.weixin.qq.com/sns/oauth2/access_token?appid=#34 . $appid . #34secret=#34 . $secret . #34code=#34 . $code . #34grant_type=authorization_code#34 $ch = curl_init() curl_setopt($ch, CURLOPT_URL, $url) curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) curl_setopt($ch, CURLOPT_TIMEOUT, 30) $content = curl_exec($ch) $status = (int)curl_get($ch, CURLINFO_HTTP_CODE) if ($status == 404) { return $status } curl_close($ch) $errcode = json_decode($content, true) if (isset($errcode[#39errcode#39])) { throw new LogicException($errcode[#39errmsg#39]) } //Log::(#39errcode:#39 . $content) if (isset($errcode[#39openid#39]) isset($errcode[#39access_token#39])) { $ = $this-gtgetUserInfo($errcode[#39openid#39], $errcode[#39access_token#39]) //Log::(#39:#39 . json_encode($, true)) $unionid = $[#39unionid#39] $nickname = $[#39nickname#39] $headimgurl = $[#39headimgurl#39] $clientSecret = $this-gtrequest-gtparam(#39clientSecret#39, #39#39) return $this-gtuserInfoService-gtlogin( #39mobile#39, $clientSecret, #39driving-test-web#39, #39oauth#39, $errcode[#39openid#39], #39#39, $this-gtrequest-gtdeviceId, $this-gtrequest-gtdeviceToken, $this-gtrequest-gtip(), $errcode[#39openid#39], $nickname, $headimgurl, $unionid ) } throw new LogicException(#39获取失败#39)}/ 微信小程序获取openid @return array|int @throws DataNotFoundException @throws DbException @throws LogicException @throws ModelNotFoundException /public function allOpenid(){ $code = $this-gtrequest-gtparam(#39code#39, #39#39) $nickname = $this-gtrequest-gtparam(#39nickname#39, #39#39) $headimgurl = $this-gtrequest-gtparam(#39headimgurl#39, #39#39) if (empty($code)) throw new LogicException(#34code必传#34) $secret = Config::get(#39business.wechat. all_secret#39) $appid = Config::get(#39business.wechat. all_appid#39) $url = #34https://api.weixin.qq.com/sns/jscode2session?appid=#34 . $appid . #34secret=#34 . $secret . #34js_code=#34 . $code . #34grant_type=authorization_code#34 $ch = curl_init() curl_setopt($ch, CURLOPT_URL, $url) curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) curl_setopt($ch, CURLOPT_TIMEOUT, 30) $content = curl_exec($ch) $status = (int)curl_get($ch, CURLINFO_HTTP_CODE) if ($status == 404) { return $status } curl_close($ch) $result = json_decode($content, true) if (isset($result[#39errcode#39])) { throw new LogicException($result[#39errmsg#39]) } //return $result[#39openid#39] $clientSecret = $this-gtrequest-gtparam(#39clientSecret#39, #39#39) return $this-gtuserInfoService-gtlogin( #39mobile#39, $clientSecret, #39driving-test-wechat#39, #39oauth#39, $result[#39openid#39], #39#39, $this-gtrequest-gtdeviceId, $this-gtrequest-gtdeviceToken, $this-gtrequest-gtip(), $result[#39openid#39], $nickname, $headimgurl, $result[#39unionid#39] )}/ 头条获取openid @return array|int @throws DataNotFoundException @throws DbException @throws LogicException @throws ModelNotFoundException /public function toutiaoOpenid(){ $code = $this-gtrequest-gtparam(#39code#39, #39#39) $nickname = $this-gtrequest-gtparam(#39nickname#39, #39#39) $headimgurl = $this-gtrequest-gtparam(#39headimgurl#39, #39#39) if (empty($code)) throw new LogicException(#34code必传#34) $secret = Config::get(#39business.toutiao.secret#39) $appid = Config::get(#39business.toutiao.app_id#39) $url = #34https://developer.toutiao.com/api/apps/jscode2session?appid=#34 . $appid . #34secret=#34 . $secret . #34code=#34 . $code $ch = curl_init() curl_setopt($ch, CURLOPT_URL, $url) curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) curl_setopt($ch, CURLOPT_TIMEOUT, 30) $content = curl_exec($ch) $status = (int)curl_get($ch, CURLINFO_HTTP_CODE) if ($status == 404) { return $status } curl_close($ch) $result = json_decode($content, true) if ($result[#39error#39] != 0) { throw new LogicException($result[#39errcode#39] . #39:#39 . $result[#39errmsg#39]) } //return $result[#39openid#39] //Log::(#39字节小程序openid:#39 . $result[#39openid#39].#39unionid:#39.$result[#39unionid#39]) $clientSecret = $this-gtrequest-gtparam(#39clientSecret#39, #39#39) return $this-gtuserInfoService-gtlogin( #39mobile#39, $clientSecret, #39driving-test-tiktok#39, #39oauth#39, $result[#39openid#39], #39#39, $this-gtrequest-gtdeviceId, $this-gtrequest-gtdeviceToken, $this-gtrequest-gtip(), $result[#39openid#39], $nickname, $headimgurl, $result[#39unionid#39] )}/ 获取用户信息 @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的Openid @return string /public function getUserInfo($openid, $access_token){ $response = self::curlGet(#39https://api.weixin.qq.com/sns/user?access_token=#39 . $access_token . #39openid=#39 . $openid . #39lang=zh_CN#39) return json_decode($response, true)}public static function curlGet($url = #39#39, $options = array()){ $ch = curl_init($url) curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) curl_setopt($ch, CURLOPT_TIMEOUT, 30) if (!empty($options)) { curl_setopt_array($ch, $options) } //https请求 不验证证书和host curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false) $data = curl_exec($ch) curl_close($ch) return $data}

公众号openid与小程序openid关联 公众号获取openid流程

Copyright@2015-2025 www.xinxueguanw.cn 心血管健康网版板所有