返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >PHP抖音小程序登录接口及微信和抖音数据互通
  • 364
分享到

PHP抖音小程序登录接口及微信和抖音数据互通

php小程序微信 2023-09-02 06:09:50 364人浏览 薄情痞子
摘要

小程序静默登录 public function douyinlogin() { $config = Config::get('douyin'); $code = $this->request

小程序静默登录

        public function douyinlogin()    {        $config = Config::get('douyin');        $code = $this->request->param('code');        $anonymous_code = $this->request->param('anonymous_code');        $phone = $this->request->param('phone');        if (empty($code) && empty($anonymous_code)) {            $this->error('参数错误');        }        $params = [            "appid" => $config['appid'],            "secret" => $config['secret'],            "anonymous_code" => $anonymous_code,            "code" => $code        ];        $configs = new Resource();        $url = "https://developer.toutiao.com/api/apps/v2/jscode2session";        $res = $configs->json_post($url, $params);        $res = json_decode($res, true);        if ($res['err_no'] == 0) {            if (isset($res['data']['openid'])) {                $userId = Db::name('third')->where(['apptype' => 'douyin', 'openid' => $res['data']['openid']])->value('user_id');//                dump($userId);                $auth = Auth::instance();                $ret = $auth->direct($userId);//                dump($ret);                if ($ret) {  //假如登录上后获取信息                    $data = $auth->getUserinfo();                    $aut = \db('user')->where('id', $userId)->find();                    $data['avatar'] = cdnurl($data['avatar']);  //cdnurl第三方存储                    $data['mobile'] = $aut['mobile'];                    $this->success('登录成功', $data);                } elseif (empty($ret)) {//                    dump(2220);                                        //根据输入的手机号查询用户                    $find = \db('user')->where('mobile', $phone)->find();                    if (empty($find)) {    //用户表没记录                        $data = ['nickname' => '抖音养殖户','username' => '抖音养殖户','avatar' => '/assets/img/avatar.png','status' => 'normal','mobile' => $phone,'jointime' => time(),'createtime' => time(),'platform' => 2                        ];                        //插入user                        $id = \db('user')->insertGetId($data);                    } else {  //用户表有记录                        $data = ['nickname' => '抖音微信养殖户','username' => '抖音微信养殖户','platform' => 3                        ];                        //插入user                        \db('user')->where('id', $find['id'])->update($data);                        $id = $find['id'];                    }                    \db('third')->insert(['user_id' => $id, 'apptype' => 'douyin', 'openname' => '抖音用户',                        'access_token' => $res['data']['session_key'], 'openid' => $res['data']['openid']]);                    $userId = Db::name('third')->where(['apptype' => 'douyin', 'openid' => $res['data']['openid']])->value('user_id');                    $auth = Auth::instance();                    $ret = $auth->direct($userId);                    if ($ret) {                        $data = $auth->getUserinfo();                        $aut = \db('user')->where('id', $userId)->find();                        $data['avatar'] = cdnurl($data['avatar']);                        $data['mobile'] = $aut['mobile'];                        $this->success('登录成功', $data);                    } else {                        $this->error('连接失败');                    }                }            } else {                $this->error('获取openid失败');            }        } else {            $this->error($res['err_tips']);        }    }
function JSON_post($url, $data = [])    {        //$data=Http_build_query($data);        //$data=json_encode($data);        $curl = curl_init();        curl_setopt($curl, CURLOPT_URL, $url);        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);        if(!$data){            return 'data is null';        }        if(is_array($data))        {            $data = json_encode($data);        }        curl_setopt($curl, CURLOPT_POST, 1);        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);        curl_setopt($curl, CURLOPT_HEADER, 0);        curl_setopt($curl, CURLOPT_HTTPHEADER,array(            'Content-Type: application/json; charset=utf-8',            'Content-Length:' . strlen($data),            'Cache-Control: no-cache',            'Pragma: no-cache'        ));        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        $res = curl_exec($curl);        $errorno = curl_errno($curl);        if ($errorno) {            return $errorno;        }        curl_close($curl);        return $res;    }

数据互通是通过手机号判断,上述代码中有示例

原则:用户表中一条数据,第三方表中两条数据对应一个用户ID

来源地址:https://blog.csdn.net/qq_53457276/article/details/131375568

--结束END--

本文标题: PHP抖音小程序登录接口及微信和抖音数据互通

本文链接: https://lsjlt.com/news/390037.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作