PHP对接阿里云虚拟号的实现(号码隐私保护)

fastadmin 封装框架

实现功能:AXN隐私号绑定、解绑;

场景:为店铺手机号开通虚拟号,用户联系店铺展示虚拟号码;

官方开放文档地址:https://help.aliyun.com/document_detail/59655.html?spm=a2c4g.11174283.6.661.b8365d50CKM5Ma

效果如下:

参考官网开放文档,下载SDK;解压得到 aliyun-dypls-php-sdk 文件夹;

将解压的文件夹内的 api_sdk 文件夹与 api_demo 文件夹内的 PlsDemo.php,拷贝到项目 vendor 目录下,笔者这里是放在了yinsi文件夹

随后打开 PlsDemo.php文件,修改 getAcsClient 方法内对应的 AccessKeyId、AccessKeySecret

还是PlsDemo.php文件,修改绑定时的号码池

还是PlsDemo.php文件,修改解绑时的号码池

最后在用户列表控制器内,添加以下代码:

php 复制代码
/**
   * 编辑
   */
  public function edit($ids = null)
  {
    $row = $this->model->get($ids);
    if (!$row) {
      $this->error(__('No Results were found'));
    }
    $adminIds = $this->getDataLimitAdminIds();
    if (is_array($adminIds)) {
      if (!in_array($row[$this->dataLimitField], $adminIds)) {
        $this->error(__('You have no permission'));
      }
    }
    if ($this->request->isPost()) {
      $params = $this->request->post("row/a");
      if ($params) {
//        print_r($params);exit;
        //开通虚拟号
        if ($params['privacydata'] == '2'){
          vendor("yinsi.PlsDemo");
          $response = \PlsDemo::bindAxn($params['mobile']) ;
          if ($response->Code == 'OK'){
            $params['privacy_tel'] = $response->SecretBindDTO->SecretNo;
            $params['subsid'] = $response->SecretBindDTO->SubsId;
          }else{
            $this->error($response->Message);
          }
        }else{
          vendor("yinsi.PlsDemo");
          $response = \PlsDemo::unbindSubscription($params['subsid'],$params['privacy_tel']) ;
          if ($response->Code == 'OK'){
            $params['privacy_tel'] = '';
            $params['subsid'] = '';
          }
        };
 
        $params = $this->preExcludeFields($params);
        $result = false;
        Db::startTrans();
        try {
          //是否采用模型验证
          if ($this->modelValidate) {
            $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
            $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
            $row->validateFailException(true)->validate($validate);
          }
          $result = $row->allowField(true)->save($params);
          Db::commit();
        } catch (ValidateException $e) {
          Db::rollback();
          $this->error($e->getMessage());
        } catch (PDOException $e) {
          Db::rollback();
          $this->error($e->getMessage());
        } catch (Exception $e) {
          Db::rollback();
          $this->error($e->getMessage());
        }
        if ($result !== false) {
          $this->success();
        } else {
          $this->error(__('No rows were updated'));
        }
      }
      $this->error(__('Parameter %s can not be empty', ''));
    }
    $this->view->assign("row", $row);
    return $this->view->fetch();
  }

与原始的修改方法区别如下接口描述:

不需要后台管理的笔友,用以下代码:

简单的通过接口绑定、解绑虚拟号。

php 复制代码
  //隐私号码绑定
  public function yinsi(){
    $type = $this->request->request('type');//1绑定2解绑
    $mobile = $this->request->request('mobile');//需要绑定的手机号
    vendor("yinsi.PlsDemo");
    $response = \PlsDemo::bindAxn($mobile) ;
    print_r($response);exit;//打印接口返回参数
  }
  //隐私号码解绑
  public function Unbundling(){
    $subsid = $this->request->request('subsid');//绑定接口中返回的subsId;
    $secretNo = $this->request->request('secretNo');//绑定接口中返回的secretNo;
    vendor("yinsi.PlsDemo");
    $response = \PlsDemo::unbindSubscription($subsid,$secretNo) ;
    print_r($response);exit;//打印接口返回参数
  }
相关推荐
翼龙云_cloud19 小时前
国际云代理商:2026年国际云注册风控升级实战指南 8 大平台无卡解决方案对比
服务器·阿里云·云计算
阿里云大数据AI技术1 天前
全模态、多引擎、一体化,阿里云DLF3.0构建Data+AI驱动的智能湖仓平台
人工智能·阿里云·云计算
摇滚侠1 天前
阿里云安装的 Redis 在什么位置,如何找到 Redis 的安装位置
redis·阿里云·云计算
m0_694845571 天前
tinylisp 是什么?超轻量 Lisp 解释器编译与运行教程
服务器·开发语言·云计算·github·lisp
ESBK20251 天前
第四届移动互联网、云计算与信息安全国际会议(MICCIS 2026)二轮征稿启动,诚邀全球学者共赴学术盛宴
大数据·网络·物联网·网络安全·云计算·密码学·信息与通信
_运维那些事儿2 天前
VM环境的CI/CD
linux·运维·网络·阿里云·ci/cd·docker·云计算
人间打气筒(Ada)2 天前
k8s:CNI网络插件flannel与calico
linux·云原生·容器·kubernetes·云计算·k8s
小白考证进阶中2 天前
阿里云ACA热门科目有哪些?考什么内容?
阿里云·阿里云认证·云计算运维·阿里云aca证书·阿里云aca认证·阿里云aca·aca证书
主机哥哥2 天前
2026年阿里云五种方案快速部署 OpenClaw(Clawdbot)详细教程
阿里云·云计算
m0_694845572 天前
music-website 是什么?前后端分离音乐网站部署实战
linux·运维·服务器·云计算·github