PHP隐私保护通话,AXB模式_隐私保护通话 PrivateNumber_开发指南_代码样例_PHP代码样例_华为云...

// 必填,请参考"开发准备"获取如下数据,替换为实际值

$realUrl = 'https://rtcapi.cn-north-1.myhuaweicloud.com:12543/rest/caas/relationnumber/partners/v1.0'; // APP接入地址+接口访问URI

$APP_KEY = 'a1d1f50cad21415fbdd13d8f53d36d60'; // APP_Key

$APP_SECRET = 'cfc881cc704c4fba8d8fef5788e03e6b'; // APP_Secret

$relationNum = '+8617010000001'; // X号码(隐私号码)

$callerNum = '+8618612345678'; // A号码

$calleeNum = '+8618612345679'; // B号码

/*

* 选填,各参数要求请参考"AXB模式绑定接口"

*/

// $areaCode = '0755'; // 需要绑定的X号码对应的城市码

// $callDirection = 0; // 允许呼叫的方向

// $duration = 86400; // 绑定关系保持时间,单位为秒。到期后会被系统自动解除绑定关系

// $recordFlag = 'false'; // 是否需要针对该绑定关系产生的所有通话录音

// $recordHintTone = 'recordHintTone.wav'; // 设置录音提示音

// $maxDuration = 60; // 设置允许单次通话进行的最长时间,单位为分钟。通话时间从接通被叫的时刻开始计算

// $lastMinVoice = 'lastMinVoice.wav'; // 设置通话剩余最后一分钟时的提示音

// $privateSms = 'true'; // 设置该绑定关系是否支持短信功能

// $callerHintTone = 'callerHintTone.wav'; // 设置A拨打X号码时的通话前等待音

// $calleeHintTone = 'calleeHintTone.wav'; // 设置B拨打X号码时的通话前等待音

// $preVoice = [

// 'callerHintTone' => $callerHintTone,

// 'calleeHintTone' => $calleeHintTone

// ];

// 请求Headers

$headers = [

'Accept: application/json',

'Content-Type: application/json;charset=UTF-8',

'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',

'X-WSSE: ' . buildWsseHeader($APP_KEY, $APP_SECRET)

];

// 请求Body,可按需删除选填参数

$data = json_encode([

'relationNum' => $relationNum,

// 'areaCode' => $areaCode,

'callerNum' => $callerNum,

'calleeNum' => $calleeNum,

// 'callDirection' => $callDirection,

// 'duration' => $duration,

// 'recordFlag' => $recordFlag,

// 'recordHintTone' => $recordHintTone,

// 'maxDuration' => $maxDuration,

// 'lastMinVoice' => $lastMinVoice,

// 'privateSms' => $privateSms,

// 'preVoice' => $preVoice

]);

$context_options = [

'http' => [

'method' => 'POST', // 请求方法为POST

'header' => $headers,

'content' => $data,

'ignore_errors' => true // 获取错误码,方便调测

],

'ssl' => [

'verify_peer' => false,

'verify_peer_name' => false

] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题

];

try {

$file=fopen('bind_data.txt', 'a'); //打开文件

print_r($data . PHP_EOL); // 打印请求数据

fwrite($file, '绑定请求数据:' . $data . PHP_EOL); //绑定请求参数记录到本地文件,方便定位问题

$response = file_get_contents($realUrl, false, stream_context_create($context_options)); // 发送请求

print_r($response . PHP_EOL); // 打印响应结果

fwrite($file, '绑定结果:' . $response . PHP_EOL); //绑定ID很重要,请记录到本地文件,方便后续修改绑定关系及解绑

} catch (Exception $e) {

echo $e->getMessage();

} finally {

fclose($file); //关闭文件

}

/**

* 构建X-WSSE值

*

* @param string $appKey

* @param string $appSecret

* @return string

*/

function buildWsseHeader($appKey, $appSecret) {

date_default_timezone_set("UTC");

$Created = date('Y-m-d\TH:i:s\Z'); //Created

$nonce = uniqid(); //Nonce

$base64 = base64_encode(hash('sha256', ($nonce . $Created . $appSecret), TRUE)); //PasswordDigest

return sprintf("UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"", $appKey, $base64, $nonce, $Created);

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是干什么用的# -*- coding: utf-8 -*- import time import uuid import hashlib import base64 import ssl import urllib.request import hmac from hashlib import sha256 # 必填,请参考"开发准备"获取如下数据,替换为实际值 realUrl = 'https://rtcpns.cn-north-1.myhuaweicloud.com/rest/caas/relationnumber/partners/v1.0' #APP接入地址+接口访问URI APP_KEY = "a1********" #APP_Key APP_SECRET = "cfc8********" #APP_Secret ''' 选填,各参数要求请参考"AXB模式解绑接口" subscriptionId和relationNum为二选一关系,两者都携带时以subscriptionId为准 ''' subscriptionId = '****' #指定"AXB模式绑定接口"返回的绑定ID进行解绑 relationNum = '+86170****0001' #指定X号码(隐私号码)进行解绑 def buildAKSKHeader(appKey, appSecret): now = time.strftime('%Y-%m-%dT%H:%M:%SZ') #Created nonce = str(uuid.uuid4()).replace('-','') #Nonce digist = hmac.new(appSecret.encode(), (nonce + now).encode(), digestmod=sha256).digest() digestBase64 = base64.b64encode(digist).decode() #PasswordDigest return 'UsernameToken Username="{}",PasswordDigest="{}",Nonce="{}",Created="{}"'.format(appKey, digestBase64, nonce, now); def main(): # 请求URL参数 formData = urllib.parse.urlencode({ 'subscriptionId':subscriptionId, 'relationNum':relationNum }) #完整请求地址 fullUrl = realUrl + '?' + formData req = urllib.request.Request(url=fullUrl, method='DELETE') #请求方法为DELETE # 请求Headers参数 req.add_header('Authorization', 'AKSK realm="SDP",profile="UsernameToken",type="Appkey"') req.add_header('X-AKSK', buildAKSKHeader(APP_KEY, APP_SECRET)) req.add_header('Content-Type', 'application/json;charset=UTF-8') # 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 ssl._create_default_https_context = ssl._create_unverified_context try: print(formData) #打印请求数据 r = urllib.request.urlopen(req) #发送请求 print(r.read().decode('utf-8')) #打印响应结果 except urllib.error.HTTPError as e: print(e.code) print(e.read().decode('utf-8')) #打印错误信息 except urllib.error.URLError as e: print(e.reason) if __name__ == '__main__': main()
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值