更新时间:2025-08-11 GMT+08:00
代码样例
"语音验证码场景API”代码样例
voiceVerificationCode.php
<?php
include 'data.php';
include 'util.php';
/**
* voiceVerificationCodeAPI
* @param string $displayNbr
* @param string $calleeNbr
* @param integer $languageType
* @param string $preTone
* @param string $verifyCode
*/
function voiceVerificationCodeAPI($displayNbr, $calleeNbr, $languageType, $preTone, $verifyCode) {
if (empty($displayNbr) || empty($calleeNbr) || empty($languageType) || empty($preTone) || empty($verifyCode)) {
return;
}
$method = 'POST';
$apiUri = '/rest/httpsessions/callVerify/v1.0';
$xaksk = buildAKSKHeader(getCallverify_AppId(), getCallverify_Secret());
$content = json_encode([
/* 必填参数*/
'displayNbr' => $displayNbr,//主叫用户手机终端的来电显示号码。
'calleeNbr' => $calleeNbr,//发起呼叫时所拨打的被叫号码。
'languageType' => $languageType,//验证码播放的语言类型。
'preTone' => $preTone,//播放语音验证码之前需要播放的放音文件名。
'verifyCode' => $verifyCode//验证码:只支持0~9的数字,最大8位。
/* 选填参数*/
// 'bindNbr' => '+86123456789', //CallEnabler业务号码,即绑定号码
// 'posTone' => 'postone.wav', //播放语音验证码之后需要播放的放音文件名
// 'times' => 3, //播放次数:0~9
// 'statusUrl' => '', //设置SP接收状态上报的URL,要求使用BASE64编码
// 'feeUrl' => '', //设置SP接收话单上报的URL,要求使用BASE64编码
// 'returnIdlePort' => 'false', //指示是否需要返回平台空闲呼叫端口数量
// 'userData' => 'customerId123' //设置用户的附属信息
]);
$requestUrl = getBaseUrl() . $apiUri;
$context_options = createOptions($method, $xaksk, $content);
try {
$response = file_get_contents($requestUrl, false, stream_context_create($context_options)); // 发送请求
print_r($response . PHP_EOL); // 打印响应结果
} catch (Exception $e) {
echo $e->getMessage(); //打印错误信息
}
}
voiceVerificationCodeAPI('+8653159511234', '+8613500000001', 2, 'test.wav', '1234');
?>
“呼叫状态通知API”代码样例
callEventImpl.php
<?php
/**
* 呼叫事件通知
* 客户平台收到RTC业务平台的呼叫事件通知的接口通知
*/
//呼叫事件通知样例
$jsonBody = json_encode([
'eventType' => 'callout',
'statusInfo' => [
'sessionId' => '1201_612_4294967295_20190124030424@callenabler245.huaweicaas.com',
'timestamp' => '2019-01-24 03:04:24',
'caller' => '+8613800000022',
'called' => '+8613800000021'
]
]);
print_r($jsonBody . PHP_EOL);
onCallEvent($jsonBody);//呼叫事件处理
/**
* 呼叫事件通知
* @desc 详细内容以接口文档为准
* @param jsonBody
*/
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); //打印通知事件类型
//callout:呼出事件
if (strcasecmp($eventType, 'callout') == 0) {
/**
* Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
*
* 'timestamp': 该呼叫事件发生时RTC业务平台的UNIX时间戳
* 'userData': 用户附属信息
* 'sessionId': 通话链路的标识ID
* 'caller': 主叫号码
* 'called': 被叫号码
*/
if (array_key_exists('sessionId', $statusInfo)) {
print_r('sessionId: ' . $statusInfo['sessionId'] . PHP_EOL);
}
return;
}
//alerting:振铃事件
if (strcasecmp($eventType, 'alerting') == 0) {
/**
* Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
*
* 'timestamp': 该呼叫事件发生时RTC业务平台的UNIX时间戳
* 'userData': 用户附属信息
* 'sessionId': 通话链路的标识ID
* 'caller': 主叫号码
* 'called': 被叫号码
*/
if (array_key_exists('sessionId', $statusInfo)) {
print_r('sessionId: ' . $statusInfo['sessionId'] . PHP_EOL);
}
return;
}
//answer:应答事件
if (strcasecmp($eventType, 'answer') == 0) {
/**
* Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
*
* 'timestamp': 该呼叫事件发生时RTC业务平台的UNIX时间戳
* 'userData': 用户附属信息
* 'sessionId': 通话链路的标识ID
* 'caller': 主叫号码
* 'called': 被叫号码
*/
if (array_key_exists('sessionId', $statusInfo)) {
print_r('sessionId: ' . $statusInfo['sessionId'] . PHP_EOL);
}
return;
}
//collectInfo:放音收号结果事件,仅应用于语音通知场景
if (strcasecmp($eventType, 'collectInfo') == 0) {
/**
* Example: 此处以解析digitInfo为例,请按需解析所需参数并自行实现相关处理
*
* 'timestamp': 该呼叫事件发生时RTC业务平台的UNIX时间戳
* 'sessionId': 通话链路的标识ID
* 'digitInfo': 放音收号场景中,RTC业务平台对开发者进行放音收号操作的结果描述
*/
if (array_key_exists('digitInfo', $statusInfo)) {
print_r('digitInfo: ' . $statusInfo['digitInfo'] . PHP_EOL);
}
return;
}
//disconnect:挂机事件
if (strcasecmp($eventType, 'disconnect') == 0) {
/**
* Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
*
* 'timestamp': 该呼叫事件发生时RTC业务平台的UNIX时间戳
* 'userData': 用户附属信息
* 'sessionId': 通话链路的标识ID
* 'caller': 主叫号码
* 'called': 被叫号码
* 'partyType': 挂机的用户类型,仅在语音回呼场景携带
* 'stateCode': 通话挂机的原因值
* 'stateDesc': 通话挂机的原因值的描述
*/
if (array_key_exists('sessionId', $statusInfo)) {
print_r('sessionId: ' . $statusInfo['sessionId'] . PHP_EOL);
}
return;
}
}
?>
“话单通知API”代码样例
feeImpl.php
<?php
/**
* 话单通知
* 客户平台收到RTC业务平台的话单通知的接口通知
*/
//话单通知样例
$jsonBody = json_encode([
'eventType' => 'fee',
'feeLst' => [
[
'direction' => 0, //通话的呼叫方向,以RTC业务平台为基准
'spId' => 'CaaS_Test_01', //客户的云服务账号
'appKey' => 'ka4kESI5s3YyurL1wpx63s9YnEm2', //应用的app_key
'icid' => 'CAE-20190124110424-12019775', //呼叫记录的唯一标识
'bindNum' => '+8675512345678', //发起此次呼叫的CallEnabler业务号码,即绑定号码
'sessionId' => '1201_612_4294967295_20190124030424@callenabler245.huaweicaas.com', //通话链路的唯一标识
'callerNum' => '+8613800000022', //主叫号码
'calleeNum' => '+8613800000021', //被叫号码
'fwdDisplayNum' => '+8613800000022', //转接呼叫时的显示号码(仅语音回呼场景携带)
'fwdDstNum' => '+8613866887021', //转接呼叫时的转接号码(仅语音回呼场景携带)
'fwdStartTime' => '2019-01-24 03:04:31', //转接呼叫操作的开始时间(仅语音回呼场景携带)
'fwdAlertingTime' => '2019-01-24 03:04:36', //转接呼叫操作后的振铃时间(仅语音回呼场景携带)
'fwdAnswerTime' => '2019-01-24 03:04:38', //转接呼叫操作后的应答时间(仅语音回呼场景携带)
'callEndTime' => '2019-01-24 03:04:49', //呼叫结束时间
'fwdUnaswRsn' => 0, //转接呼叫操作失败的Q850原因值
'failTime' => '', //呼入,呼出的失败时间
'ulFailReason' => 0, //通话失败的拆线点
'sipStatusCode' => 0, //呼入,呼出的失败SIP状态码
'callOutStartTime' => '2019-01-24 03:04:24', //Initcall的呼出开始时间
'callOutAlertingTime' => '2019-01-24 03:04:27', //Initcall的呼出振铃时间
'callOutAnswerTime' => '2019-01-24 03:04:31', //Initcall的呼出应答时间
'callOutUnaswRsn' => 0, //Initcall的呼出失败的Q850原因值
'dynIVRStartTime' => '', #自定义动态IVR开始时间(仅语音通知场景携带)
'dynIVRPath' => '', //自定义动态IVR按键路径(仅语音通知场景携带)
'recordFlag' => 0, //录音标识
'recordStartTime' => '', //录音开始时间(仅语音回呼场景携带)
'recordObjectName' => '', //录音文件名(仅语音回呼场景携带)
'recordBucketName' => '', //录音文件所在的目录名(仅语音回呼场景携带)
'recordDomain' => '', //存放录音文件的域名(仅语音回呼场景携带)
'recordFileDownloadUrl' => '', //录音文件下载地址(仅语音回呼场景携带)
'ttsPlayTimes' => 0, //应用TTS功能时,使用TTS的总次数
'ttsTransDuration' => 0, //应用TTS功能时,TTS Server进行TTS转换的总时长(单位为秒)
'serviceType' => '002', //携带呼叫的业务类型信息
'hostName' => 'callenabler245.huaweicaas.com', //话单生成的服务器设备对应的主机名
'userData' => '' //用户附属信息
]
]
]);
print_r($jsonBody . PHP_EOL);
onFeeEvent($jsonBody);//话单处理
/**
* 话单通知
* @desc 详细内容以接口文档为准
* @param jsonBody
*/
function onFeeEvent($jsonBody) {
$jsonArr = json_decode($jsonBody, true); //将通知消息解析为关联数组
$eventType = $jsonArr['eventType']; //通知事件类型
if (strcasecmp($eventType, 'fee') != 0) {
print_r('EventType error: ' . $eventType);
return;
}
if (!array_key_exists('feeLst', $jsonArr)) {
print_r('param error: no feeLst.');
return;
}
$feeLst = $jsonArr['feeLst']; //呼叫话单事件信息
//Example: 此处以解析sessionId为例,请按需解析所需参数并自行实现相关处理
//短时间内有多个通话结束时RTC业务平台会将话单合并推送,每条消息最多携带50个话单
if (sizeof($feeLst) > 1) {
foreach ($feeLst as $loop){
if (array_key_exists('sessionId', $loop)) {
print_r('sessionId: ' . $loop['sessionId'] . PHP_EOL);
}
}
} else if(sizeof($feeLst) == 1) {
if (array_key_exists('sessionId', $feeLst[0])) {
print_r('sessionId: ' . $feeLst[0]['sessionId'] . PHP_EOL);
}
} else {
print_r('feeLst error: no element.');
}
}
?>
父主题: PHP