3.2 PHP 代码样例
3.2.2 AX 模式
//print_r($jsonArr['appKey'] . PHP_EOL); //商户应用的AppKey $smsEvent = $jsonArr['smsEvent']; //短信通知信息
/**
* Example: 此处以解析notificationMode为例,请按需解析所需参数并自行实现相关处理 *
* 'smsIdentifier': 短信唯一标识 * 'notificationMode': 通知模式 * 'calling': 真实发送方号码 * 'called': 真实接收方号码
* 'virtualNumber': 隐私号码(X号码) * 'event': 短信状态事件
* 'timeStamp': 短信事件发生的系统时间戳,UTC时间 * 'subscriptionId': 绑定ID
* 'smsContent': 用户发送的短信内容 */
if (array_key_exists('notificationMode', $smsEvent)) {
if (strcasecmp($smsEvent['notificationMode'], 'Block') == 0) {
//收到隐私保护通话平台的短信通知,若为Block模式,请参考接口文档回消息指示下一步操作 $actions = [
'operation' => 'vNumberRoute', //操作类型:转发短信/丢弃短信 'message' => [
'called' => '+86138****7022', //真实接收方号码 'calling' => '+86138****7021' //真实发送方号码 ]
];
$resp = json_encode(['actions' => [$actions]]); //Block模式响应消息 print_r($resp . PHP_EOL);
} elseif (strcasecmp($smsEvent['notificationMode'], 'Notify') == 0) {
//收到隐私保护通话平台的短信通知,若为Notify模式,请回HTTP状态码为200的空消息 //http_response_code(200);
print_r('This is AXB sms Notify mode.');
} else {
print_r('notificationMode param error.');
} } }?>
3.2.2 AX 模式
样例
AX模式绑定接口
AX模式解绑接口
AX模式绑定信息修改接口 AX模式绑定信息查询接口 AX模式设置临时被叫接口
获取录音文件下载地址接口 呼叫事件通知接口话单通知接口 短信通知接口
环境要求 基于PHP 7.2.9版本,要求PHP 5.6及以上版本。
须知
● 本文档所述Demo在提供服务的过程中,可能会涉及个人数据的使用,建议您遵从 国家的相关法律采取足够的措施,以确保用户的个人数据受到充分的保护。
● 本文档所述Demo仅用于功能演示,不允许客户直接进行商业使用。
● 本文档信息仅供参考,不构成任何要约或承诺。
● 本文档接口携带参数只是用作参考,不可以直接复制使用,填写参数需要替换为实 际值,请参考“开发准备”获取所需数据。
AX 模式绑定接口
<?php
// 必填,请参考"开发准备"获取如下数据,替换为实际值
$realUrl = 'https://rtcpns.cn-north-1.myhuaweicloud.com/rest/provision/caas/privatenumber/v1.0'; // APP接入 地址+接口访问URI
$APP_KEY = 'a1********'; // APP_Key
$APP_SECRET = 'cfc8****************'; // APP_Secret
$origNum = '+86186****5678'; // A号码
$privateNum = '+86170****0001'; // X号码(隐私号码) /* * 选填,各参数要求请参考"AX模式绑定接口"
*/// $privateNumType = 'mobile-virtual'; //固定为mobile-virtual // $areaCode = '0755'; //需要绑定的X号码对应的城市码
// $recordFlag = 'false'; //是否需要针对该绑定关系产生的所有通话录音 // $recordHintTone = 'recordHintTone.wav'; //设置录音提示音
$calleeNumDisplay = '0'; // 设置非A用户呼叫X时,A接到呼叫时的主显号码 // $privateSms = 'true'; //设置该绑定关系是否支持短信功能
// $callerHintTone = 'callerHintTone.wav'; //设置A拨打X号码时的通话前等待音 // $calleeHintTone = 'calleeHintTone.wav'; //设置非A用户拨打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([
'origNum' => $origNum, 'privateNum' => $privateNum,
// 'privateNumType' => $privateNumType, // 'areaCode' => $areaCode,
// 'recordFlag' => $recordFlag,
// 'recordHintTone' => $recordHintTone, 'calleeNumDisplay' => $calleeNumDisplay, // '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);
}?>
AX 模式解绑接口
<?php
// 必填,请参考"开发准备"获取如下数据,替换为实际值
$realUrl = 'https://rtcpns.cn-north-1.myhuaweicloud.com/rest/provision/caas/privatenumber/v1.0'; // APP接入 地址+接口访问URI
$APP_KEY = 'a1********'; // APP_Key
$APP_SECRET = 'cfc8****************'; // APP_Secret /* * 选填,各参数要求请参考"AX模式解绑接口"
* subscriptionId和(origNum+privateNum)为二选一关系,都携带时以subscriptionId为准 */$origNum = '+86186****5678'; // A号码
$privateNum = '+86170****0001'; // X号码
// $subscriptionId = '********'; // 指定"AX模式绑定接口"返回的绑定ID进行解绑 // 请求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)
];// 请求URL参数
$data = http_build_query([
'origNum' => $origNum, 'privateNum' => $privateNum // 'subscriptionId' => $subscriptionId
]);// 完整请求地址
$fullUrl = $realUrl . '?' . $data;
$context_options = [ 'http' => [
'method' => 'DELETE', // 请求方法为DELETE 'header' => $headers,
'ignore_errors' => true // 获取错误码,方便调测 ],
'ssl' => [
'verify_peer' => false, 'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 ];
try {
print_r($data . PHP_EOL); // 打印请求数据
$response = file_get_contents($fullUrl, false, stream_context_create($context_options)); // 发送请求 print_r($response . PHP_EOL); // 打印响应结果
} catch (Exception $e) { echo $e->getMessage();
}
/** * 构建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);
}?>
AX 模式绑定信息修改接口
<?php
// 必填,请参考"开发准备"获取如下数据,替换为实际值
$realUrl = 'https://rtcpns.cn-north-1.myhuaweicloud.com/rest/provision/caas/privatenumber/v1.0'; // APP接入 地址+接口访问URI
$APP_KEY = 'a1********'; // APP_Key
$APP_SECRET = 'cfc8****************'; // APP_Secret /* * 选填,各参数要求请参考"AX模式绑定信息修改接口"
* subscriptionId和(origNum+privateNum)为二选一关系,都携带时以subscriptionId为准 */$origNum = '+86186****5678'; // A号码
$privateNum = '+86170****0001'; // X号码
$subscriptionId = '********'; // 指定"AX模式绑定接口"返回的绑定ID进行修改
$privateSms = 'true'; // 必填,修改该绑定关系是否支持短信功能 // 请求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([
'origNum' => $origNum, 'privateNum' => $privateNum, 'subscriptionId' => $subscriptionId, 'privateSms' => $privateSms ]);
$context_options = [ 'http' => [
'method' => 'PUT', // 请求方法为PUT 'header' => $headers,
'content' => $data,
'ignore_errors' => true // 获取错误码,方便调测 ],
'ssl' => [
'verify_peer' => false, 'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 ];
try {
print_r($data . PHP_EOL); // 打印请求数据
$response = file_get_contents($realUrl, false, stream_context_create($context_options)); // 发送请求 print_r($response . PHP_EOL); // 打印响应结果
} catch (Exception $e) { echo $e->getMessage();
}
/** * 构建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);
}?>
AX 模式绑定信息查询接口
<?php
// 必填,请参考"开发准备"获取如下数据,替换为实际值
$realUrl = 'https://rtcpns.cn-north-1.myhuaweicloud.com/rest/provision/caas/privatenumber/v1.0'; // APP接入 地址+接口访问URI
$APP_KEY = 'a1********'; // APP_Key
$APP_SECRET = 'cfc8****************'; // APP_Secret /* * 选填,各参数要求请参考"AX模式绑定信息查询接口"
* subscriptionId和origNum为二选一关系,两者都携带时以subscriptionId为准 */$origNum = '+86186****5678'; // 指定A号码进行查询
$subscriptionId = '********'; // 指定"AX模式绑定接口"返回的绑定ID进行查询 // 请求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) ];// 请求URL参数
$data = http_build_query([
'origNum' => $origNum,
'subscriptionId' => $subscriptionId ]);// 完整请求地址
$fullUrl = $realUrl . '?' . $data;
$context_options = [ 'http' => [
'method' => 'GET', // 请求方法为GET 'header' => $headers,
'ignore_errors' => true // 获取错误码,方便调测 ],
'ssl' => [
'verify_peer' => false, 'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 ];
try {
$file=fopen('bind_data.txt', 'a'); //打开文件
$response = file_get_contents($fullUrl, false, stream_context_create($context_options)); // 发送请求 print_r($response . PHP_EOL); // 打印响应结果
fwrite($file, '绑定查询结果:' . $response . PHP_EOL); //查询结果,记录到本地文件 } 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);
}?>
AX 模式设置临时被叫接口
<?php
// 必填,请参考"开发准备"获取如下数据,替换为实际值
$realUrl = 'https://rtcpns.cn-north-1.myhuaweicloud.com/rest/caas/privatenumber/calleenumber/v1.0'; //
APP接入地址+接口访问URI
$APP_KEY = 'a1********'; // APP_Key
$APP_SECRET = 'cfc8****************'; // APP_Secret /* * 选填,各参数要求请参考"AX模式设置临时被叫接口"
* subscriptionId和(origNum+privateNum)为二选一关系,都携带时以subscriptionId为准 */$origNum = '+86186****5678'; // A号码
$privateNum = '+86170****0001'; // X号码
// $subscriptionId = '********'; // 指定"AX模式绑定接口"返回的绑定ID设置临时被叫
$calleeNum = '+86186****5679'; // 必填,本次呼叫的真实被叫号码,origNum和calleeNum不能相同 // 请求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([
'origNum' => $origNum, 'privateNum' => $privateNum, // 'subscriptionId' => $subscriptionId, 'calleeNum' => $calleeNum ]);
$context_options = [ 'http' => [
'method' => 'PUT', // 请求方法为PUT 'header' => $headers,
'content' => $data,
'ignore_errors' => true // 获取错误码,方便调测 ],
'ssl' => [
'verify_peer' => false, 'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题 ];
try {
print_r($data . PHP_EOL); // 打印请求数据
$response = file_get_contents($realUrl, false, stream_context_create($context_options)); // 发送请求 print_r($response . PHP_EOL); // 打印响应结果
} catch (Exception $e) { echo $e->getMessage();
}
/** * 构建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);
}?>
获取录音文件下载地址接口
<?php
// 必填,请参考"开发准备"获取如下数据,替换为实际值
$realUrl = 'https://rtcpns.cn-north-1.myhuaweicloud.com/rest/provision/voice/record/v1.0'; // APP接入地址 +接口访问URI
$APP_KEY = 'a1********'; // APP_Key
$APP_SECRET = 'cfc8****************'; // APP_Secret // 必填,通过"话单通知接口"获取
$recordDomain = '****.com';
$fileName = '****.wav';
// 请求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)
];// 请求URL参数
$data = http_build_query([
'recordDomain' => $recordDomain, 'fileName' => $fileName
]);// 完整请求地址
$fullUrl = $realUrl . '?' . $data;
$context_options = [ 'http' => [
'method' => 'GET', // 请求方法为GET 'header' => $headers,
'max_redirects' => '0', // 关闭重定向
'ignore_errors' => true // 获取错误码,方便调测
],// 为防止因代码工具重定向导致获取内容乱码,需要关闭重定向.若PHP是7.1.0及以上版本,若不需要可注释掉 'ssl' => [
'verify_peer' => false, 'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
];//若PHP版本介于5.1.3和7.1.0之间,请使用如下代码.若PHP是7.1.0及以上版本,请注释掉如下代码
// stream_context_set_default([
// 'ssl' => [
// 'verify_peer' => false, // 'verify_peer_name' => false // ]
// ]);
try {
$file=fopen('bind_data.txt', 'a'); //打开文件
//若PHP版本介于5.1.3和7.1.0之间,请使用如下代码获取响应消息头域信息 //$http_response_header = get_headers($fullUrl, 1); //获取响应消息头域信息 //若PHP是7.1.0及以上版本,请使用如下代码获取响应消息头域信息
$http_response_header = get_headers($fullUrl, 1, stream_context_create($context_options)); //获取响应消 息头域信息
if(strpos($http_response_header[0], '301') !== false){
foreach ($http_response_header as $loop){
if(strpos($loop, "Location") !== false){
$fileUrl = trim(substr($loop, 10));
print_r($fileUrl); //获取录音文件下载地址
fwrite($file, '获取录音文件下载地址:' . $fileUrl . PHP_EOL); //查询结果,记录到本地文件 }
} }else{
print_r($http_response_header[0] . PHP_EOL); //打印响应码400/401/403/500
// $response = file_get_contents($fullUrl, false, stream_context_create($context_options)); //获取响应消 息数据信息
// print_r($response . PHP_EOL); // 打印响应结果 }
} 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);
}?>
呼叫事件通知接口
<?php
/** * 呼叫事件通知
* 客户平台收到隐私保护通话平台的呼叫事件通知的接口通知 */
//呼叫事件通知样例
$jsonBody = json_encode([
'eventType' => 'disconnect', 'statusInfo' => [
'sessionId' => '1200_1827_4294967295_20190124023003@callenabler246.huaweicaas.com', 'timestamp' => '2019-01-24 02:30:22',
'caller' => '+86138****0022', 'called' => '+86138****7021', 'stateCode' => 0,
'stateDesc' => 'The user releases the call.', 'subscriptionId' => '********'
] ]);
print_r($jsonBody . PHP_EOL);
//呼叫事件处理
onCallEvent($jsonBody);
/** * 呼叫事件通知
* @desc 详细内容以接口文档为准 * @param jsonArr
*/function onCallEvent($jsonBody) {
$jsonArr = json_decode($jsonBody, true); //将通知消息解析为关联数组 $eventType = $jsonArr['eventType']; //通知事件类型
if (strcasecmp($eventType, 'fee') == 0) { print_r('EventType error: ' . $eventType);
return;
}
if (!array_key_exists('statusInfo', $jsonArr)) { print_r('param error: no statusInfo.');
return;
}
$statusInfo = $jsonArr['statusInfo']; //呼叫状态事件信息
print_r('eventType: ' . $eventType . PHP_EOL); //打印通知事件类型 //callin:呼入事件
if (strcasecmp($eventType, 'callin') == 0) { /**
* Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理 *
* 'timestamp': 呼叫事件发生时隐私保护通话平台的UNIX时间戳 * 'sessionId': 通话链路的标识ID
* 'caller': 主叫号码 * 'called': 被叫号码
* 'subscriptionId': 绑定关系ID */
if (array_key_exists('sessionId', $statusInfo)) {
print_r('sessionId: ' . $statusInfo['sessionId'] . PHP_EOL);
if (array_key_exists('sessionId', $statusInfo)) {
print_r('sessionId: ' . $statusInfo['sessionId'] . PHP_EOL);
} return;
}
//alerting:振铃事件
if (strcasecmp($eventType, 'alerting') == 0) { /**
if (array_key_exists('sessionId', $statusInfo)) {
print_r('sessionId: ' . $statusInfo['sessionId'] . PHP_EOL);
if (array_key_exists('sessionId', $statusInfo)) {
print_r('sessionId: ' . $statusInfo['sessionId'] . PHP_EOL);
} return;
}
//disconnect:挂机事件
if (strcasecmp($eventType, 'disconnect') == 0) { /**
if (array_key_exists('sessionId', $statusInfo)) {
print_r('sessionId: ' . $statusInfo['sessionId'] . PHP_EOL);
} return;
} }?>
话单通知接口
<?php /** * 话单通知
* 客户平台收到隐私保护通话平台的话单通知的接口通知 */
//话单通知样例
$jsonBody = json_encode([
'eventType' => 'fee', 'feeLst' => [ [
'direction' => 1, 'spId' => '********', 'appKey' => '********',
'icid' => 'ba171f34e6953fcd751edc77127748f4.3757285803.338666679.5', 'bindNum' => '+86138****0022',
'sessionId' => '1200_1827_4294967295_20190124023003@callenabler246.huaweicaas.com', 'subscriptionId' => '********',
'callerNum' => '+86138****0021', 'calleeNum' => '+86138****0022', 'fwdDisplayNum' => '+86138****0022', 'fwdDstNum' => '+86138****7021', 'callInTime' => '2019-01-24 02:30:03', 'fwdStartTime' => '2019-01-24 02:30:03', 'fwdAlertingTime' => '2019-01-24 02:30:04',
'callerNum' => '+86138****0021', 'calleeNum' => '+86138****0022', 'fwdDisplayNum' => '+86138****0022', 'fwdDstNum' => '+86138****7021', 'callInTime' => '2019-01-24 02:30:03', 'fwdStartTime' => '2019-01-24 02:30:03', 'fwdAlertingTime' => '2019-01-24 02:30:04',