阿里云虚拟电话保护

admin3年前PHP597
  1. 下载安装好compser

  2. 下载DEMO,参考网址https://next.api.aliyun.com/api/Dyplsapi/2017-05-25/BindAxb?spm=a2c4g.11186623.0.0.7a834581kzRfns&lang=PHP&params=%7B%7D,并解压到某个目录

  3. 进入DEMO目录,执行 composer install && php src/Sample.php

  4. 搭建代码,如下

    Sample.php

  5. <?php
    
    // This file is auto-generated, don't edit it. Thanks.
    namespace AlibabaCloud\SDK\Sample;
    
    use AlibabaCloud\SDK\Dyplsapi\V20170525\Dyplsapi;
    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\SDK\Dyplsapi\V20170525\Models\UnbindSubscriptionRequest;
    use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
    
    function object_to_array($obj){
            $_arr=is_object($obj)?get_object_vars($obj):$obj;
            $arr = null;
            foreach($_arr as $key=>$val){
                $val=(is_array($val))||is_object($val)?object_to_array($val):$val;
                $arr[$key]=$val;
            }
            return $arr;
        }
    
    class Sample {
    
        /**
         * 使用AK&SK初始化账号Client
         * @param string $accessKeyId
         * @param string $accessKeySecret
         * @return Dyplsapi Client
         */
        public static function createClient($accessKeyId, $accessKeySecret){
            $config = new Config([
                // 您的 AccessKey ID
                "accessKeyId" => $accessKeyId,
                // 您的 AccessKey Secret
                "accessKeySecret" => $accessKeySecret
            ]);
            // 访问的域名
            $config->endpoint = "dyplsapi.aliyuncs.com";
            return new Dyplsapi($config);
        }
        
        
    
        /**
         * @param string[] $args
         * @return void
         */
        public static function BindAxb($args){
            $client = self::createClient($args['ak'], $args['sk']);
            $bindAxbRequest = new BindAxbRequest($args);
            $runtime = new RuntimeOptions($args);
            try {
                $res = $client->bindAxbWithOptions($bindAxbRequest, $runtime);
                if($res->body->code != 'OK') {
                    return ['code' => 0, 'errcode' => $res->body->code, 'des' => $res->body->message];
                }else {
                    return ['code' => 1, 'extension' => $res->body->secretBindDTO->extension, 'secretNo' => $res->body->secretBindDTO->secretNo, 'subsId' => $res->body->secretBindDTO->subsId];
                }
            }
            catch (Exception $error) {
                if (!($error instanceof TeaError)) {
                    $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
                }
                return ['code' => 0, 'errcode' => $error->getCode(), 'des' => $error->getMessage() ];
                // 如有需要,请打印 error
                Utils::assertAsString($error->message);
            }
        }
        
        /**
         * @param string[] $args
         * @return void
         */
        public static function UnbindSubscription($args){
            $client = self::createClient($args['ak'], $args['sk']);
            $unbindSubscriptionRequest = new UnbindSubscriptionRequest($args);
            $runtime = new RuntimeOptions($args);
            try {
                $res = $client->unbindSubscriptionWithOptions($unbindSubscriptionRequest, $runtime);
                if($res->body->code != 'OK') {
                    return ['code' => 0, 'errcode' => $res->body->code, 'des' => $res->body->message];
                }else {
                    return ['code' => 1, 'extension' => $res->body->secretBindDTO->extension, 'secretNo' => $res->body->secretBindDTO->secretNo, 'subsId' => $res->body->secretBindDTO->subsId];
                }
            }
            catch (Exception $error) {
                if (!($error instanceof TeaError)) {
                    $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
                }
                return ['code' => 0, 'errcode' => $error->getCode(), 'des' => $error->getMessage() ];
                // 如有需要,请打印 error
                Utils::assertAsString($error->message);
            }
        }
    }
    $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
    if (file_exists($path)) {
        require_once $path;
    }
    $key = '111';
    if(empty($_GET)) {
        echo json_encode(['code' => 0, 'errcode' => '非法访问' ]);
        exit();
    }
    $signature = trim($_GET['signature']);
    $args = trim($_GET['args']);
    if(md5($args&$key) != $signature) {
        echo json_encode(['code' => 0, 'errcode' => '非法访问' ]);
        exit();
    }
    $args = json_decode($_GET['args'], true);
    $type = $args['type'];
    unset($args['type']);
    switch($type) {
        //添加关系
        case 'BindAxb':
            $res = Sample::BindAxb($args);
            break;
        //解绑
        case 'UnbindSubscription':
            $res = Sample::UnbindSubscription($args);
            break;
    }
    print_r(json_encode($res));
  6. run.php
  7. //绑定号码
        public function bindPhone($PhoneNoA, $PhoneNoB) {
            //五分钟内失效
            $expiration = date('Y-m-d H:i:s', time() + intval(PHONE_ETIME)*60 );
            $phones = VIRTUAL_PHONE;
            if(empty($phones)) {
                $this->ejson(0, CUSTOMER_CONTACT);
            }
            $sphones = explode(',', $phones);
            foreach($sphones as $v) {
                $PhoneNoX = $v;
                $args = [
                    "type" => "BindAxb",
                    "ak" => SMS_AK,
                    "sk" => SMS_SK,
                    "poolKey" => PHONE_KEY,
                    "phoneNoA" => $PhoneNoA,
                    "phoneNoB" => $PhoneNoB,
                    "phoneNoX" => $PhoneNoX,
                    "expiration" => $expiration
                ];
                $args = json_encode($args);
                $signature = md5($args&$this->key);
                $res = minihttpGet('https://www.baidu.com/axb/src/Sample.php?args='.$args.'&signature='.$signature);
                if(!empty($res)) {
                    $res = json_decode($res, true);
                    if($res['code'] == 1) {
                        //成功写入数据库,并返回手机号码
                        $data['PhoneNoA'] = $PhoneNoA;
                        $data['PhoneNoB'] = $PhoneNoB;
                        $data['PhoneNoX'] = $PhoneNoX;
                        $data['Expiration'] = $res['extension'];
                        $data['secretNo'] = $res['secretNo'];
                        $data['SubsId'] = $res['subsId'];
                        $data['add_time'] = time();
                        $bool = $this->House_phones_relationship_model->add($data);
                        if($bool) {
                            return $PhoneNoX;
                        }
                    }
                }
            }
        }
        
        //设置电话隐私保护
        public function setVirtualPhone() {
            $PhoneNoA = $this->post('vmobile');
            if(empty($PhoneNoA)) {
                $this->ejson(-1, '卖家电话错误');
            }
            $PhoneNoB = $this->appusers['mobile'];
            if(empty($PhoneNoB)) {
                $this->ejson(-1, '会员电话错误');
            }
            if(empty(PHONE_KEY) || empty(VIRTUAL_PHONE) || empty(SMS_AK) || empty(SMS_SK) ) {
                $this->ejson(-1, '参数错误');
            }
            //先判断号码B是否绑定,如果有解绑
            $this->load->model('House_phones_relationship_model');
            $checkRes = $this->House_phones_relationship_model->find(array('PhoneNoB' => $PhoneNoB));
            if(!empty($checkRes)) {
                $args = [
                    "type" => 'UnbindSubscription',
                    "ak" => SMS_AK,
                    "sk" => SMS_SK,
                    "poolKey" => PHONE_KEY,
                    "subsId" => $checkRes['SubsId'],
                    "secretNo" => $checkRes['secretNo']
                ];
                $args = json_encode($args);
                $signature = md5($args&$this->key);
                $res = minihttpGet('https://www.baidu.com/axb/src/Sample.php?args='.$args.'&signature='.$signature);
                if(!empty($res)) {
                    $res = json_decode($res, true);
                    if($res['code'] == 1) {
                        //删除数据库记录
                        $bool = $this->House_phones_relationship_model->del($checkRes['id']);
                        $phone = $this->bindPhone($PhoneNoA, $PhoneNoB);
                        if(empty($phone)) {
                            $this->ejson(0, '');
                        }else {
                            $this->ejson(1, $phone);
                        }
                    }else {
                        $this->ejson(0, CUSTOMER_CONTACT);
                    }
                }
            }else {
                $phone = $this->bindPhone($PhoneNoA, $PhoneNoB);
                if(empty($phone)) {
                    $this->ejson(0, '');
                }else {
                    $this->ejson(1, $phone);
                }
            }
            
    
        }

相关文章

redis

进入CMD命令进入相应redis目录cd redisredis-server.exe redis.windows.conf...

Yii2 手册

https://www.kancloud.cn/moxuandi/yii2-class...

宝塔面板无法执行composer命令

请选择root用户执行...

PayPal Payments Standard如何设置?

paypal账户登录后,依次点击setting - 账户设置 - 产品与服务 - 网站付款 - 网站习惯设定自动返回开启,填写URL付款数据传输 开启创建sandbox账号https://develo...

opencart paypal 体验账号测试支付

opencart paypal 体验账号测试支付

很多朋友在设置独立站后台Paypal收款时,经常看到Live和Sandbox这两个字眼。是否会产生疑惑:这是干什么的呢?其实这里的LIVE模式是正常付款环境下的付款模式,Sandbox就是我们所说的沙...

yii2框架怎么写子查询

在yii中使用子查询的方法第一步,先创建一个子查询,可以是基于 yii\db\Query 创建,也可以基于Model。$subQuery = Order::find()->where(['...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。