#PHP##小程序##微信小程序##抖音##抖音小程序# ,下面我们就来说一说关于公众号openid怎么获取?我们一起去了解并探讨一下这个问题吧!
公众号openid怎么获取
/**
* 微信公众号联合登录
* @return array|int
* @throws DataNotFoundException
* @throws DbException
* @throws LogicException
* @throws ModelNotFoundException
*/
public function openid(): array
{
$code = $this->request->param('code', '');
if (empty($code))
throw new LogicException("code必传");
$secret = Config::get('business.wechat.secret');
$appid = Config::get('business.wechat.appid');
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $secret . "&code=" . $code . "&grant_type=authorization_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_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status == 404) {
return $status;
}
curl_close($ch);
$errcode = json_decode($content, true);
if (isset($errcode['errcode'])) {
throw new LogicException($errcode['errmsg']);
}
//Log::info('errcode:' . $content);
if (isset($errcode['openid']) && isset($errcode['access_token'])) {
$info = $this->getUserInfo($errcode['openid'], $errcode['access_token']);
//Log::info('info:' . json_encode($info, true));
$unionid = $info['unionid'];
$nickname = $info['nickname'];
$headimgurl = $info['headimgurl'];
$clientSecret = $this->request->param('clientSecret', '');
return $this->userInfoService->login(
'mobile',
$clientSecret,
'driving-test-web',
'oauth',
$errcode['openid'],
'',
$this->request->deviceId,
$this->request->deviceToken,
$this->request->ip(),
$errcode['openid'],
$nickname,
$headimgurl,
$unionid
);
}
throw new LogicException('获取失败');
}
/**
* 微信小程序获取openid
* @return array|int
* @throws DataNotFoundException
* @throws DbException
* @throws LogicException
* @throws ModelNotFoundException
*/
public function smallOpenid()
{
$code = $this->request->param('code', '');
$nickname = $this->request->param('nickname', '');
$headimgurl = $this->request->param('headimgurl', '');
if (empty($code))
throw new LogicException("code必传");
$secret = Config::get('business.wechat.small_secret');
$appid = Config::get('business.wechat.small_appid');
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_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_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status == 404) {
return $status;
}
curl_close($ch);
$result = json_decode($content, true);
if (isset($result['errcode'])) {
throw new LogicException($result['errmsg']);
}
//return $result['openid'];
$clientSecret = $this->request->param('clientSecret', '');
return $this->userInfoService->login(
'mobile',
$clientSecret,
'driving-test-wechat',
'oauth',
$result['openid'],
'',
$this->request->deviceId,
$this->request->deviceToken,
$this->request->ip(),
$result['openid'],
$nickname,
$headimgurl,
$result['unionid']
);
}
/**
* 头条获取openid
* @return array|int
* @throws DataNotFoundException
* @throws DbException
* @throws LogicException
* @throws ModelNotFoundException
*/
public function toutiaoOpenid()
{
$code = $this->request->param('code', '');
$nickname = $this->request->param('nickname', '');
$headimgurl = $this->request->param('headimgurl', '');
if (empty($code))
throw new LogicException("code必传");
$secret = Config::get('business.toutiao.secret');
$appid = Config::get('business.toutiao.app_id');
$url = "https://developer.toutiao.com/api/apps/jscode2session?appid=" . $appid . "&secret=" . $secret . "&code=" . $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_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status == 404) {
return $status;
}
curl_close($ch);
$result = json_decode($content, true);
if ($result['error'] != 0) {
throw new LogicException($result['errcode'] . ':' . $result['errmsg']);
}
//return $result['openid'];
//Log::info('字节小程序openid:' . $result['openid'].';unionid:'.$result['unionid']);
$clientSecret = $this->request->param('clientSecret', '');
return $this->userInfoService->login(
'mobile',
$clientSecret,
'driving-test-tiktok',
'oauth',
$result['openid'],
'',
$this->request->deviceId,
$this->request->deviceToken,
$this->request->ip(),
$result['openid'],
$nickname,
$headimgurl,
$result['unionid']
);
}
/**
* 获取用户信息
* @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的Openid
* @return string
*/
public function getUserInfo($openid, $access_token)
{
$response = self::curlGet('https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN');
return json_decode($response, true);
}
public static function curlGet($url = '', $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;
}