返回
快速导航关闭
当前搜索
网站分类
栏目推荐
实用工具
热门标签
子分类:
创奇学院 >微信 >公众号 >微信公众号 » 正文

怎么制作精美的公众号文章,微信公众号联合登录&微信小程序获取openid&头条获取openid

微信公众号 更新时间: 发布时间: 微信公众号归档 最新发布 网站地图

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

 /**
 * 微信公众号联合登录
 * @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;
}

转载请注明:文章转载自 http://www.320g.com/
本文地址:http://www.320g.com/wxgzh/30256.html
考高分网交流群

扫一扫加入QQ交流群

和更多志同道合朋友一起交流,分享干货资料!
创奇学院客服

扫一扫加客服微信

有疑问请咨询创奇学院微信号,在线为您解答!
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 Copyright 320g.com Rights Reserved. Powered · 创奇学院

ICP备案号:陇ICP备2020003353号