thinkphp对接阿里云号码隐私保护笔记

随着越来越多用户对隐私保护的关注,现在开发软件大多需要我们对用户的手机号码进行保护。因此阿里云的号码隐私保护也成了众多开发者的首选。本篇笔记主要记录对接过程的代码片段及遇到的问题解决。

阿里云号码隐私文档:点击这里

对接步骤:

1、安装openapi类库:

  1. composer require alibabacloud/darabonba-openapi:0.2.*
  2. composer require alibabacloud/dyplsapi-20170525:1.0.*

2、关键方法封装:

  1. <?
  2. namespace  自定义
  3. use AlibabaCloud\SDK\Dyplsapi\V20170525\Dyplsapi;
  4. use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\QuerySubscriptionDetailRequest;
  5. use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\UnbindSubscriptionRequest;
  6. use \Exception;
  7. use AlibabaCloud\Tea\Exception\TeaError;
  8. use AlibabaCloud\Tea\Utils\Utils;
  9. use Darabonba\OpenApi\Models\Config;
  10. use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\BindAxbRequest;
  11. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  12. class Privacy{
  13. /**
  14.      * 使用AK&SK初始化账号Client
  15.      * @param string $accessKeyId
  16.      * @param string $accessKeySecret
  17.      * @return Dyplsapi Client
  18.      */
  19.     public static function createClient(){
  20.         $config = new Config([
  21.             // 您的 AccessKey ID
  22.             "accessKeyId" => '',
  23.             // 您的 AccessKey Secret
  24.             "accessKeySecret" => ''
  25.         ]);
  26.         // 访问的域名
  27.         $config->endpoint = "dyplsapi.aliyuncs.com";
  28.         return new Dyplsapi($config);
  29.     }
  30.     /**
  31.      * @param string[] $args
  32.      * $args示例 ["poolKey" => "","phoneNoA" => "","phoneNoB" => "","expiration" => "10"]
  33.      * @return array
  34.      */
  35.     public static function bindAxb($args){
  36.         $client = self::createClient();
  37.         $bindAxbRequest = new BindAxbRequest($args);
  38.         $runtime = new RuntimeOptions([]);
  39.         try {
  40.             // 复制代码运行请自行打印 API 的返回值
  41.             $res = $client->bindAxbWithOptions($bindAxbRequest$runtime);
  42.             $body = get_object_vars($res->body);
  43.             //data_print($body);
  44.             if($body['code'] != 'OK'){
  45.                 return ['code'=>0,'message'=>'获取隐私号失败'];
  46.             }
  47.             $secretBindDTO = get_object_vars($body['secretBindDTO']);
  48.             $secretBindDTO['code'] = 1;
  49.             return $secretBindDTO;
  50.         }catch (Exception $error) {
  51.             return ['code'=>0,'message'=>$error->getMessage()];
  52.         }
  53.     }
  54.     //查询号码绑定关系
  55.     //["subsId" => "", //绑定关系ID"poolKey" => "", //号码池Key "phoneNoX" => "" //隐私号码]
  56.     public static function QuerySubscriptionDetail($args){
  57.         $client = self::createClient();
  58.         $querySubscriptionDetailRequest = new QuerySubscriptionDetailRequest($args);
  59.         $runtime = new RuntimeOptions([]);
  60.         try {
  61.             // 复制代码运行请自行打印 API 的返回值
  62.             $res = $client->querySubscriptionDetailWithOptions($querySubscriptionDetailRequest$runtime);
  63.             $body = get_object_vars($res->body);
  64.             return $body;
  65.         }catch (Exception $error) {
  66.             if (!($error instanceof TeaError)) {
  67.                 $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  68.             }
  69.             // 如有需要,请打印 error
  70.             Utils::assertAsString($error->message);
  71.         }
  72.     }
  73.     //解绑隐私号
  74.     /**
  75.      * @param string[] $args
  76.      * @return void
  77.      */
  78.     public static function UnbindSubscription($args){
  79.         $client = self::createClient();
  80.         $unbindSubscriptionRequest = new UnbindSubscriptionRequest($args);
  81.         $runtime = new RuntimeOptions([]);
  82.         try {
  83.             // 复制代码运行请自行打印 API 的返回值
  84.             $res = $client->unbindSubscriptionWithOptions($unbindSubscriptionRequest$runtime);
  85.             $body = get_object_vars($res->body);
  86.             //data_print($body);
  87.             if(strtolower($body['code']) == 'ok'){
  88.                 return true;
  89.             }
  90.             return false;
  91.         }catch (Exception $error) {
  92.             return false;
  93.         }
  94.     }
  95. }

3、调用方法:

  1. //绑定关系
  2. $args = ["poolKey" => $poolKey,"phoneNoA" => $from['mobile_a'],"phoneNoB" => $to['mobile_b'],"expiration" => $expiration];
  3. $result = Privacy::bindAxb($args);
  4. //解绑关系
  5. $args = ['poolKey'=>$poolKey,'subsId'=>$log['subs_id'],'secretNo'=>$log['privacy_mobile']];
  6. $rtn = Privacy::UnbindSubscription($args);

4、通知回调:

需要在双方通话结束后处理的逻辑,可以通过通话回调进行实现。

  1. public function privacy(){
  2.         $post = input('post.');
  3.         if(!emptyempty($post)){
  4.             foreach ($post as $key=>$item){
  5.                 $log = PrivacyLogsModel::where(['subs_id'=>$item['sub_id']])->find();
  6.                 if($log){
  7.                     $arr = ['status'=>5,'record_file'=>$item['ring_record_url']??'','call_id'=>$item['call_id'],
  8.                         'release_dir'=>$item['release_dir'],'is_unbind'=>1,'call_type'=>$item['call_type'],
  9.                         'call_time'=>$item['call_time']??'','ring_time'=>$item['ring_time']??'',
  10.                         'start_time'=>$item['start_time']??'','release_time'=>$item['release_time']??'',
  11.                         'unbind_time'=>date('Y-m-d H:i:s')];
  12.                     //释放隐私号
  13.                     $args = ['poolKey'=>$poolKey,'subsId'=>$item['sub_id'],'secretNo'=>$item['secret_no']];
  14.                     Privacy::UnbindSubscription($args);
  15.                     $log->save($arr);
  16.                 }
  17.             }
  18.         }
  19.         return json(['code'=>0,'msg'=>'成功']);
  20.     }

更多参数可以参考官方文档。

你想把广告放到这里吗?

发表评论

您必须 登录 才能发表留言!