• php实现支付宝授权登录


    第一步:

    登录到蚂蚁金服开放平台 https://open.alipay.com/platform/home.htm,前提是有商户号。创建应用之后,然后到开发者中心开通对应功能。如图:

    第二步:

    到应用信息里面填写应用网关与授权回调地址。应用网关填写域名即可,如:www.baidu.com;授权回调地址则需要具体到页面或者方法,如:www.baidu.com/aliLogin.php。

    第三步:

    填写接口加签方式。去官方下载签名生成器,然后生成一对秘钥(应用公钥和应用私钥),设置好应用公钥之后,能拿到对应的支付宝公钥。(重点是 应用私钥和支付宝公钥)

    第四步:

    数据表的设计。可以新建数据表存储支付宝返回数据,也可以在用户表里面新加字段存储。我的实现方法是后者,如图:

    下面代码可以看到支付宝返回数据存入对应字段

    第五步:

    下载sdk,地址   https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.iw1InW&treeId=193&articleId=103419&docType=1 。根据以上步骤拿到的appid,应用私钥(RSA_PRIVATE_KEY)和支付宝公钥(ALIPAY_RSA_PBULIC_KEY),开始调用支付宝接口获取数据。上代码:

     1 /**
     2      * 支付宝授权登录
     3      */
     4     public function aliLogin(){
     5         //获取配置文件的ali参数
     6         $ali_config = C("ALI_CONFIG");
     7 
     8         //应用的APPID
     9         $app_id = "2017061407485473";
    10         //【成功授权】后的回调地址
    11         $my_url = "http://".$_SERVER['HTTP_HOST']."/Home/User/aliLogin";
    12 
    13         //Step1:获取auth_code
    14         $auth_code = $_REQUEST["auth_code"];//存放auth_code
    15         if(empty($auth_code)){
    16             //state参数用于防止CSRF攻击,成功授权后回调时会原样带回
    17             $_SESSION['alipay_state'] = md5(uniqid(rand(), TRUE));
    18             //拼接请求授权的URL
    19             $url = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=".$app_id."&scope=auth_user&redirect_uri=".$my_url."&state="
    20                 . $_SESSION['alipay_state'];
    21 
    22             echo("<script> top.location.href='" . $url . "'</script>");
    23         }
    24         //Step2: 使用auth_code换取apauth_token
    25         if($_REQUEST['state'] == $_SESSION['alipay_state'] || 1)
    26         {
    27             vendor("Alipay.AopClient"); //引入sdk
    28             $aop = new AopClient();
    29             $aop->gatewayUrl             = "https://openapi.alipay.com/gateway.do";
    30             $aop->appId                   = $app_id;
    31             $aop->rsaPrivateKey           = $ali_config['RSA_PRIVATE_KEY'];  //应用私钥
    32             $aop->alipayrsaPublicKey   = $ali_config['ALIPAY_RSA_PBULIC_KEY'];  //支付宝公钥
    33             $aop->apiVersion              = '1.0';
    34             $aop->signType                = 'RSA2';
    35             $aop->postCharset             = 'utf-8';
    36             $aop->format                    = 'json';
    37 
    38             //根据返回的auth_code换取access_token
    39             vendor("Alipay.AlipaySystemOauthTokenRequest");  //调用sdk里面的AlipaySystemOauthTokenRequest类
    40             $request = new AlipaySystemOauthTokenRequest();
    41             $request->setGrantType("authorization_code");
    42             $request->setCode($auth_code);
    43             $result = $aop->execute($request);
    44             $access_token = $result->alipay_system_oauth_token_response->access_token;
    45 
    46         //Step3: 用access_token获取用户信息
    47             vendor("Alipay.AlipayUserInfoShareRequest");  //调用sdk里面的AlipayUserInfoShareRequest类
    48             $request = new AlipayUserInfoShareRequest();
    49             $result = $aop->execute ( $request, $access_token);
    50             $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
    51             $resultCode = $result->$responseNode->code;
    52             if(!empty($resultCode)&&$resultCode == 10000){
    53                 $user_data = $result->$responseNode;
    54                 $m = M("Member");
    55                 $data = array();
    56                 $data['sex']              = $user_data->gender=='m'?1:2;
    57                 $data['province']      = $user_data->province;
    58                 $data['city']             = $user_data->city;
    59                 $data['person_name']   = $user_data->nick_name;
    60                 $data['ali_openid']    = $user_data->user_id;
    61                 $data['ali_name']      = $user_data->nick_name;
    62                 $data['ali_img']       = $user_data->avatar;
    63                 $data['addtime']       = date("Y-m-d H:i:s", time());
    64                 $data['person_img']       = $user_data->avatar;
    65                 $data['signtime']       = date("Y-m-d H:i:s", time());
    66 
    67                 $user = M("Member")->where(array("ali_openid"=> $user_data->user_id))->find();
    68 
    69     //判断是否是第一次登录
    70                 if($user){
    71                     $res = M("Member")->where(array("ali_openid"=> $user_data->user_id))->setField("signtime", date("Y-m-d H:i:s", time()));
    72                     if($res){
    73                          //成功登录业务逻辑
    74                        
    75                     }else{
    76                         $this->error("操作异常,拒绝访问!", U('user/login'));
    77                     }
    78                 }else{
    79                     $res=$m->add($data);
    80                     if($res){
    81                          //成功登录业务逻辑
    82 
    83 
    84                     }else{
    85                         $this->error("操作异常,拒绝访问!", U('user/login'));
    86                     }
    87                 }
    88 
    89             } else {
    90                 $this->error("操作异常,拒绝访问!", U('user/login'));
    91             }
    92 
    93         }
    94     }
     
  • 相关阅读:
    Django学习手册
    Django学习手册
    django 学习手册
    Django学习手册
    python
    python
    osg学习笔记2, 命令行参数解析器ArgumentParser
    osg(OpenSceneGraph)学习笔记1:智能指针osg::ref_ptr<>
    Boost.Build 简明教程
    Boost1.6x+win7+VC2015编译
  • 原文地址:https://www.cnblogs.com/endv/p/7643765.html
Copyright © 2020-2023  润新知