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),开始调用支付宝接口获取数据。上代码:
/** * 支付宝授权登录 */ public function aliLogin(){ //获取配置文件的ali参数 $ali_config = C("ALI_CONFIG"); //应用的APPID $app_id = "2017061407485473"; //【成功授权】后的回调地址 $my_url = "http://".$_SERVER['HTTP_HOST']."/Home/User/aliLogin"; //Step1:获取auth_code $auth_code = $_REQUEST["auth_code"];//存放auth_code if(empty($auth_code)){ //state参数用于防止CSRF攻击,成功授权后回调时会原样带回 $_SESSION['alipay_state'] = md5(uniqid(rand(), TRUE)); //拼接请求授权的URL $url = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=".$app_id."&scope=auth_user&redirect_uri=".$my_url."&state=" . $_SESSION['alipay_state']; echo("<script> top.location.href='" . $url . "'</script>"); } //Step2: 使用auth_code换取apauth_token if($_REQUEST['state'] == $_SESSION['alipay_state'] || 1) { vendor("Alipay.AopClient"); //引入sdk $aop = new \AopClient(); $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do"; $aop->appId = $app_id; $aop->rsaPrivateKey = $ali_config['RSA_PRIVATE_KEY']; //应用私钥 $aop->alipayrsaPublicKey = $ali_config['ALIPAY_RSA_PBULIC_KEY']; //支付宝公钥 $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; //根据返回的auth_code换取access_token vendor("Alipay.AlipaySystemOauthTokenRequest"); //调用sdk里面的AlipaySystemOauthTokenRequest类 $request = new \AlipaySystemOauthTokenRequest(); $request->setGrantType("authorization_code"); $request->setCode($auth_code); $result = $aop->execute($request); $access_token = $result->alipay_system_oauth_token_response->access_token; //Step3: 用access_token获取用户信息 vendor("Alipay.AlipayUserInfoShareRequest"); //调用sdk里面的AlipayUserInfoShareRequest类 $request = new \AlipayUserInfoShareRequest(); $result = $aop->execute ( $request, $access_token); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if(!empty($resultCode)&&$resultCode == 10000){ $user_data = $result->$responseNode; $m = M("Member"); $data = array(); $data['sex'] = $user_data->gender=='m'?1:2; $data['province'] = $user_data->province; $data['city'] = $user_data->city; $data['person_name'] = $user_data->nick_name; $data['ali_openid'] = $user_data->user_id; $data['ali_name'] = $user_data->nick_name; $data['ali_img'] = $user_data->avatar; $data['addtime'] = date("Y-m-d H:i:s", time()); $data['person_img'] = $user_data->avatar; $data['signtime'] = date("Y-m-d H:i:s", time()); $user = M("Member")->where(array("ali_openid"=> $user_data->user_id))->find(); //判断是否是第一次登录 if($user){ $res = M("Member")->where(array("ali_openid"=> $user_data->user_id))->setField("signtime", date("Y-m-d H:i:s", time())); if($res){ //成功登录业务逻辑 }else{ $this->error("操作异常,拒绝访问!", U('user/login')); } }else{ $res=$m->add($data); if($res){ //成功登录业务逻辑 }else{ $this->error("操作异常,拒绝访问!", U('user/login')); } } } else { $this->error("操作异常,拒绝访问!", U('user/login')); } } }