码迷,mamicode.com
首页 > Web开发 > 详细

PHP实现支付宝登录

时间:2019-07-31 23:55:15      阅读:1088      评论:0      收藏:0      [点我收藏+]

标签:sap   https   tchar   nap   input   style   dev   lis   for   

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);
    }

 

PHP实现支付宝登录

标签:sap   https   tchar   nap   input   style   dev   lis   for   

原文地址:https://www.cnblogs.com/shineguang/p/11279709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!