首页 > 编程知识 正文

阿里云服务器php一键配置

时间:2023-05-05 14:28:14 阅读:197994 作者:4345

本接口仅适用于一键登录/注册场景,需最终用户经过一键登录SDK提供的授权页确认授权后方可调用。

阿里云一键登录取号api文档:

https://help.aliyun.com/document_detail/115500.html?spm=a2c4g.11186623.6.568.33c74daaoTnCrQ

阿里云一键登录取号在线获取demo代码:

https://api.aliyun.com/new?spm=a2c4g.11186623.2.12.5f4f7861wAEpqK#/?product=Dypnsapi&version=2017-05-25&api=GetMobile&params={%22RegionId%22:%22cn-hangzhou%22}&tab=DEMO&lang=PHP

下载阿里云OpenAPI-SDK-PHP:

 

经过APP客户端的授权,向后端发起一键登录请求,后端接到请求后,使用阿里云取号接口GetMobile获取用户本机手机号,后端拿到手机号后进行一般登录/注册流程。

直接上代码:

//1、APP端请求后端登录接口public function login(){ $aliToken= app()->request->params('aliToken', '');//app端授权一键登录请求得到的token if(empty($aliToken)){ $this->responseJson(1, '参数错误', array(), true); return; } //后端拿着APP端给的token请求阿里云getmobile接口获取手机号 $aliRes = $this->aliLoginGetMobile($aliToken); if($aliRes['status'] == 'success'){ if(isset($aliRes['data']['Code']) && $aliRes['data']['Code'] == 'OK'){ $telphone = $aliRes['data']['GetMobileResultDTO']['Mobile'];//得到手机号 $user = $this->getUserByPhone($telphone); if($user){ //登录流程 }else{ //注册流程 } }else{ $this->responseJson(1, $aliRes['data']['Code'].':'.$aliRes['data']['Message'], array(), true); return; } }else{ $this->responseJson(1, $aliRes['code'].':'.$aliRes['msg'], array(), true); return; }}//2、阿里云取号接口private function aliLoginGetMobile($token = ''){ // Download:https://github.com/aliyun/openapi-sdk-php // Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md $accessKeyId = config('App.aliOauth.accessKeyId'); $accessKeySecret = config('App.aliOauth.accessKeySecret'); AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret) ->regionId('cn-hangzhou') ->asDefaultClient(); try { $result = AlibabaCloud::rpc() ->product('Dypnsapi') ->scheme('https') // https | http ->version('2017-05-25') ->action('GetMobile') ->method('POST') ->host('dypnsapi.aliyuncs.com') ->options([ 'query' => [ 'RegionId' => "cn-hangzhou", 'AccessToken' => $token ], ]) ->request(); return array('status' => 'success', 'data' => $result->toArray()); } catch (ClientException $e) { return array('status' => 'failed', 'code' => $e->getErrorCode(), 'msg' => $e->getErrorMessage()); } catch (ServerException $e) { return array('status' => 'failed', 'code' => $e->getErrorCode(), 'msg' => $e->getErrorMessage()); }}

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。