阿里云虚拟电话保护
下载安装好compser
下载DEMO,参考网址https://next.api.aliyun.com/api/Dyplsapi/2017-05-25/BindAxb?spm=a2c4g.11186623.0.0.7a834581kzRfns&lang=PHP¶ms=%7B%7D,并解压到某个目录
进入DEMO目录,执行 composer install && php src/Sample.php
搭建代码,如下
Sample.php
<?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));
run.php
//绑定号码 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); } } }