当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP实现支付宝登录

PHP实现支付宝登录

2019年07月31日  | 移动技术网IT编程  | 我要评论
1.支付宝路由拼接
    public function dev()
    {
        $http = 'https://openauth.alipaydev.com/oauth2/publicappauthorize.htm?';
        $app_id = '';
        $scope = 'auth_user';
        $redirect_uri = urlencode('');
        $state = '0';
        $url = $http . 'app_id=' . $app_id . '&scope=' . $scope . '&redirect_uri=' . $redirect_uri . '&state=' . $state;
        $this->redirect($url);
    }

2.支付宝的回调页面

public function zhi()
    {
        $params = input();
        require_once './plugins/auth/aopsdk.php';
        $aop = new \aopclient ();
        $aop->gatewayurl = 'https://openapi.alipaydev.com/gateway.do';
        $aop->appid = '2016100200645172';
        $aop->rsaprivatekey = '私钥';
        $aop->alipayrsapublickey = '公钥';
        $aop->apiversion = '1.0';
        $aop->signtype = 'rsa2';
        $aop->postcharset = 'utf-8';
        $aop->format = 'json';
        $request = new \alipaysystemoauthtokenrequest ();
        $request->setgranttype("authorization_code");
        $request->setcode($params['auth_code']);
        $result = $aop->execute($request);
        $result = (new \think\collection($result))->toarray();
        $access_token = $result['alipay_system_oauth_token_response']->access_token ?? '';
        if (!$access_token) {
            echo json_encode(['msg' => 'code无效'], json_unescaped_unicode);
        }
        $request = new \alipayuserinfosharerequest ();
        $result = $aop->execute($request, $access_token);
        \app\home\logic\auto::ali($result);
        $list = \app\common\model\user::where('open_type', 'alipay')->where('openid', $result->alipay_user_info_share_response->user_id)->find();
        session('user_info', $list->toarray());
        $blank_url = session('blank_url') ?: 'home/index/index';
        \app\home\logic\cartlogic::cookietodb();
        $this->redirect($blank_url);
    }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网