TP5.1写的api
官方接入说明:/aliyun/openapi-sdk-php/blob/master/README-zh-CN.md?spm=a2c4g.11186623.2.13.267a16e4QkHp9N&file=README-zh-CN.md
客户端对接好之后,发送请求到服务端
composer
下载PHP版sdk
composer require alibabacloud/sdk
下载的内容:
将安卓端传过来的内容用sdk换算成手机号码返回
//引入sdkuse AlibabaCloud\Client\AlibabaCloud;use AlibabaCloud\Client\Exception\ClientException;use AlibabaCloud\Client\Exception\ServerException;class User extends Comment{/** 注册/登录*/public function login(){// 获取手机token$token = input('get.token');$token = trim($token);// 判断登录token是否有效if (!$token) {//如果token为空就返回$res = ['state' => 100,'msg' => 'token不能为空,请重试'];return json_encode($res);} else {//调用getPhone方法并且将token传给getPhone$res = $this->getPhone($token);//如果返回的状态为1说明是注册过的用户if ($res['state'] == 1) {$phone = $res['phone'];//传入手机号码调用hasUser方法$data = $this->hasUser($phone);//返回给安卓端登录状态return json_encode($data);} else {//如果没有注册过就返回注册状态return json_encode($res);}}}/** 根据token换取手机号码*/public function getPhone($token){AlibabaCloud::accessKeyClient(Config::get('xianqi.phone_access_key'), Config::get('xianqi.phone_access_secret'))->regionId('cn-hangzhou')->asDefaultClient();try {$result = AlibabaCloud::rpc()->product('Dypnsapi')->scheme('https')// https | http->version('-05-25')->action('GetMobile')->method('POST')->host('')->options(['query' => ['RegionId' => "cn-hangzhou",'AccessToken' => $token],])->request();// 将返回的结果转化为数组$result = $result->toArray();//判断当前数组不为空if (isset($result['GetMobileResultDTO']['Mobile'])) {// token不为空返回手机号码$phone = $result['GetMobileResultDTO']['Mobile'];$res = ['state' => 1,'phone' => $phone];return $res;} else {//如果token为空$res = ['state' => 0,'msg' => 'token无效'];return $res;}} catch (ClientException $e) {//有异常就抛出异常// 客户端错误$res = ['state' => 101,'msg' => '注册失败'];return $res;} catch (ServerException $e) {// 服务端错误$res = ['state' => 101,'msg' => '注册失败'];return $res;}}/** 查询数据库中是否有该用户,存在则登录,不存在则注册*/public function hasUser($phone){$data = Db::table('sh_member')->where('phone', $phone)->select();// 判断该用户是注册用户还是登录用户if (empty($data)) {//如果当前用是注册用户就将内容存进数据库Db::table('sh_member')->data(['phone' => $phone])->insert();// 返回注册信息$res = ['state' => 1,'msg' => '注册成功','data' => ['phone' => $phone,'register' => true]];return $res;} else {// 如果是登录用户则将返回查询结果$phone = $data[0]['phone'];$isbuy = $data[0]['isbuy'];// 返回登录信息$res = ['state' => 1,'msg' => '登录成功','data' => ['phone' => $phone,'register' => false]];return $res;}}