随着越来越多用户对隐私保护的关注,现在开发软件大多需要我们对用户的手机号码进行保护。因此阿里云的号码隐私保护也成了众多开发者的首选。本篇笔记主要记录对接过程的代码片段及遇到的问题解决。
阿里云号码隐私文档:点击这里
对接步骤:
1、安装openapi类库:
- composer require alibabacloud/darabonba-openapi:0.2.*
- composer require alibabacloud/dyplsapi-20170525:1.0.*
2、关键方法封装:
- <?
- namespace 自定义
- use AlibabaCloud\SDK\Dyplsapi\V20170525\Dyplsapi;
- use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\QuerySubscriptionDetailRequest;
- use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\UnbindSubscriptionRequest;
- use \Exception;
- use AlibabaCloud\Tea\Exception\TeaError;
- use AlibabaCloud\Tea\Utils\Utils;
- use Darabonba\OpenApi\Models\Config;
- use AlibabaCloud\SDK\Dyplsapi\V20170525\Models\BindAxbRequest;
- use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
- class Privacy{
- /**
- * 使用AK&SK初始化账号Client
- * @param string $accessKeyId
- * @param string $accessKeySecret
- * @return Dyplsapi Client
- */
- public static function createClient(){
- $config = new Config([
- // 您的 AccessKey ID
- "accessKeyId" => '',
- // 您的 AccessKey Secret
- "accessKeySecret" => ''
- ]);
- // 访问的域名
- $config->endpoint = "dyplsapi.aliyuncs.com";
- return new Dyplsapi($config);
- }
- /**
- * @param string[] $args
- * $args示例 ["poolKey" => "","phoneNoA" => "","phoneNoB" => "","expiration" => "10"]
- * @return array
- */
- public static function bindAxb($args){
- $client = self::createClient();
- $bindAxbRequest = new BindAxbRequest($args);
- $runtime = new RuntimeOptions([]);
- try {
- // 复制代码运行请自行打印 API 的返回值
- $res = $client->bindAxbWithOptions($bindAxbRequest, $runtime);
- $body = get_object_vars($res->body);
- //data_print($body);
- if($body['code'] != 'OK'){
- return ['code'=>0,'message'=>'获取隐私号失败'];
- }
- $secretBindDTO = get_object_vars($body['secretBindDTO']);
- $secretBindDTO['code'] = 1;
- return $secretBindDTO;
- }catch (Exception $error) {
- return ['code'=>0,'message'=>$error->getMessage()];
- }
- }
- //查询号码绑定关系
- //["subsId" => "", //绑定关系ID"poolKey" => "", //号码池Key "phoneNoX" => "" //隐私号码]
- public static function QuerySubscriptionDetail($args){
- $client = self::createClient();
- $querySubscriptionDetailRequest = new QuerySubscriptionDetailRequest($args);
- $runtime = new RuntimeOptions([]);
- try {
- // 复制代码运行请自行打印 API 的返回值
- $res = $client->querySubscriptionDetailWithOptions($querySubscriptionDetailRequest, $runtime);
- $body = get_object_vars($res->body);
- return $body;
- }catch (Exception $error) {
- if (!($error instanceof TeaError)) {
- $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
- }
- // 如有需要,请打印 error
- Utils::assertAsString($error->message);
- }
- }
- //解绑隐私号
- /**
- * @param string[] $args
- * @return void
- */
- public static function UnbindSubscription($args){
- $client = self::createClient();
- $unbindSubscriptionRequest = new UnbindSubscriptionRequest($args);
- $runtime = new RuntimeOptions([]);
- try {
- // 复制代码运行请自行打印 API 的返回值
- $res = $client->unbindSubscriptionWithOptions($unbindSubscriptionRequest, $runtime);
- $body = get_object_vars($res->body);
- //data_print($body);
- if(strtolower($body['code']) == 'ok'){
- return true;
- }
- return false;
- }catch (Exception $error) {
- return false;
- }
- }
- }
3、调用方法:
- //绑定关系
- $args = ["poolKey" => $poolKey,"phoneNoA" => $from['mobile_a'],"phoneNoB" => $to['mobile_b'],"expiration" => $expiration];
- $result = Privacy::bindAxb($args);
- //解绑关系
- $args = ['poolKey'=>$poolKey,'subsId'=>$log['subs_id'],'secretNo'=>$log['privacy_mobile']];
- $rtn = Privacy::UnbindSubscription($args);
4、通知回调:
需要在双方通话结束后处理的逻辑,可以通过通话回调进行实现。
- public function privacy(){
- $post = input('post.');
- if(!emptyempty($post)){
- foreach ($post as $key=>$item){
- $log = PrivacyLogsModel::where(['subs_id'=>$item['sub_id']])->find();
- if($log){
- $arr = ['status'=>5,'record_file'=>$item['ring_record_url']??'','call_id'=>$item['call_id'],
- 'release_dir'=>$item['release_dir'],'is_unbind'=>1,'call_type'=>$item['call_type'],
- 'call_time'=>$item['call_time']??'','ring_time'=>$item['ring_time']??'',
- 'start_time'=>$item['start_time']??'','release_time'=>$item['release_time']??'',
- 'unbind_time'=>date('Y-m-d H:i:s')];
- //释放隐私号
- $args = ['poolKey'=>$poolKey,'subsId'=>$item['sub_id'],'secretNo'=>$item['secret_no']];
- Privacy::UnbindSubscription($args);
- $log->save($arr);
- }
- }
- }
- return json(['code'=>0,'msg'=>'成功']);
- }
更多参数可以参考官方文档。