安装依赖环境
安装 Composer
unix 环境在命令行中执行以下命令安装。
curl -sS https://getcomposer.org/installer | phpsudo mv composer.phar /usr/local/bin/composer
安装 SDK
composer require tencentcloud/tencentcloud-sdk-php
composer require tencentcloud/common tencentcloud/soe tencentcloud/sts
使用 SDK
本地音频文件评测
<?phprequire_once __DIR__ . '../../vendor/autoload.php';// 导入对应产品模块的clientuse TencentCloud\\Soe\\V20180724\\SoeClient;use TencentCloud\\Soe\\V20180724\\Models\\TransmitOralProcessWithInitRequest;use TencentCloud\\Common\\Exception\\TencentCloudSDKException;use TencentCloud\\Common\\Credential;try {// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取$cred = new Credential("SecretId", "SecretKey");// 实例化soe的client对象$client = new SoeClient($cred, "");// 实例化发音数据传输接口请求对象$transreq = new TransmitOralProcessWithInitRequest();// 请求参数赋值$chars = md5(uniqid(mt_rand(), true));$SessionId = substr($chars, 0, 8) . '-'. substr($chars, 8, 4) . '-'. substr($chars, 12, 4) . '-'. substr($chars, 16, 4) . '-'. substr($chars, 20, 12);$filepath = "SOEMUSIC/apple.mp3"; //本地音频文件$musicData = file_get_contents($filepath);$base64Data = base64_encode($musicData);$params = array("RefText" => "apple","WorkMode" => 1,"EvalMode" => 0,"ScoreCoeff" => 1.0,"SessionId" => $SessionId,"VoiceFileType" => 3,"VoiceEncodeType" => 1,"UserVoiceData" => $base64Data,"SeqId" => 1,"IsEnd" => 1,);$transreq->fromJsonString(json_encode($params));// 通过client对象调用TransmitOralProcessWithInit方法发起请求$transresp = $client->TransmitOralProcessWithInit($transreq);// 输出json格式的字符串回包$json_resp = $transresp->toJsonString();print_r($json_resp);} catch (TencentCloudSDKException $e) {echo $e;}
评测超时处理
如果本地文件过大,可能会导致评测超时。可以选用如下方案:
1. 流式分片传输:将发音数据进行分片处理,减少每次评测的时间。
2. 异步查询:使用异步功能将发音数据先上传,然后使用查询功能查询结构。
<?phprequire_once __DIR__ . '../../vendor/autoload.php';// 导入对应产品模块的clientuse TencentCloud\\Soe\\V20180724\\SoeClient;use TencentCloud\\Soe\\V20180724\\Models\\TransmitOralProcessWithInitRequest;use TencentCloud\\Common\\Exception\\TencentCloudSDKException;use TencentCloud\\Common\\Credential;try {// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取$cred = new Credential("SecretId", "SecretKey");// 实例化soe的client对象$client = new SoeClient($cred, "");// 实例化发音数据传输接口请求对象$transreq = new TransmitOralProcessWithInitRequest();// 请求参数赋值$chars = md5(uniqid(mt_rand(), true));$SessionId = substr($chars, 0, 8) . '-'. substr($chars, 8, 4) . '-'. substr($chars, 12, 4) . '-'. substr($chars, 16, 4) . '-'. substr($chars, 20, 12);$filepath = "SOEMUSIC/apple.mp3"; //本地音频文件$musicData = file_get_contents($filepath);$music_length = strlen($musicData);$slice_num = 100 * 1024; // 分片大小$pkgnum = ceil($music_length / $slice_num);for ($j = 1; $j <= $pkgnum; $j++) {$start_index = ($j - 1) * $slice_num;$last_index = $j == $pkgnum ? $music_length : $j * $slice_num;$IsEnd = $j == $pkgnum ? 1 : 0;$send_content = substr($musicData, $start_index, $slice_num);$base64Data = base64_encode($send_content);$params = array("RefText" => "apple","WorkMode" => 0,"EvalMode" => 0,"ScoreCoeff" => 1.0,"SessionId" => $SessionId,"VoiceFileType" => 3,"VoiceEncodeType" => 1,"UserVoiceData" => $base64Data,"SeqId" => $j,"IsEnd" => $IsEnd,);$transreq->fromJsonString(json_encode($params));// 通过client对象调用TransmitOralProcessWithInit方法发起请求$transresp = $client->TransmitOralProcessWithInit($transreq);// 输出json格式的字符串回包$json_resp = $transresp->toJsonString();print_r($json_resp);}} catch (TencentCloudSDKException $e) {echo $e;}
<?phprequire_once __DIR__ . '../../vendor/autoload.php';// 导入对应产品模块的clientuse TencentCloud\\Soe\\V20180724\\SoeClient;use TencentCloud\\Soe\\V20180724\\Models\\TransmitOralProcessWithInitRequest;use TencentCloud\\Common\\Exception\\TencentCloudSDKException;use TencentCloud\\Common\\Credential;try {// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取$cred = new Credential("SecretId", "SecretKey");// 实例化soe的client对象$client = new SoeClient($cred, "");// 实例化发音数据传输接口请求对象$transreq = new TransmitOralProcessWithInitRequest();// 请求参数赋值$chars = md5(uniqid(mt_rand(), true));$SessionId = substr($chars, 0, 8) . '-'. substr($chars, 8, 4) . '-'. substr($chars, 12, 4) . '-'. substr($chars, 16, 4) . '-'. substr($chars, 20, 12);$filepath = "SOEMUSIC/para.mp3"; //本地音频文件$musicData = file_get_contents($filepath);$base64Data = base64_encode($musicData);$params = array("RefText" => "apple","WorkMode" => 1,"EvalMode" => 2,"ScoreCoeff" => 1.0,"SessionId" => $SessionId,"VoiceFileType" => 3,"VoiceEncodeType" => 1,"UserVoiceData" => $base64Data,"SeqId" => 1,"IsEnd" => 1,"IsAsync" => 1,);$transreq->fromJsonString(json_encode($params));for ($i = 1; $i <= 60; $i++) {// 通过client对象调用TransmitOralProcess方法发起请求$transresp = $client->TransmitOralProcessWithInit($transreq);// 输出json格式的字符串回包$json_resp = $transresp->toJsonString();print_r($json_resp);$json_decode = json_decode($json_resp);$status = $json_decode->Status;if ($status == "Finished") {break;} else {$params["IsQuery"] = 1;sleep(1);}}} catch (TencentCloudSDKException $e) {echo $e;}
使用方法说明
1. SessionID:使用 uuid 来作为 SessionID,用于区分不同的音频。
2. base64 转换:读取本地音频文件,转换成 base64 数据进行评测。
3. 流式分片 base64 转换:读取本地音频文件,将音频进行分片,转换成 base64 数据进行评测。
4. 异步轮询:使用 json 反序列化,获取评测结果,判断是否查询到最终结果。
$chars = md5(uniqid(mt_rand(), true));$SessionId = substr($chars, 0, 8) . '-'. substr($chars, 8, 4) . '-'. substr($chars, 12, 4) . '-'. substr($chars, 16, 4) . '-'. substr($chars, 20, 12);
$filepath = "apple.mp3"; //填入本地音频文件路径$musicData = file_get_contents($filepath);$base64Data = base64_encode($musicData);
$musicData = file_get_contents($filepath);$music_length = strlen($musicData);$slice_num = 1 * 1024; // 分片大小$pkgnum = ceil($music_length / $slice_num);for ($j = 1; $j <= $pkgnum; $j++) {$start_index = ($j - 1) * $slice_num;$last_index = $j == $pkgnum ? $music_length : $j * $slice_num;$IsEnd = $j == $pkgnum ? 1 : 0;$send_content = substr($musicData, $start_index, $last_index);$base64Data = base64_encode($send_content);}
for ($i = 1; $i <= 60; $i++) {// 通过client对象调用TransmitOralProcess方法发起请求$transresp = $client->TransmitOralProcessWithInit($transreq);// 输出json格式的字符串回包$json_resp = $transresp->toJsonString();print_r($json_resp);$json_decode = json_decode($json_resp);$status = $json_decode->Status;if ($status == "Finished") {break;} else {$params["IsQuery"] = 1;sleep(1);}}
临时授权凭证
<?phprequire_once __DIR__ . '../../vendor/autoload.php';use TencentCloud\\Common\\Credential;use TencentCloud\\Common\\Exception\\TencentCloudSDKException;use TencentCloud\\Sts\\V20180813\\StsClient;use TencentCloud\\Sts\\V20180813\\Models\\GetFederationTokenRequest;try {// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取$cred = new Credential("SecretId", "SecretKey");$client = new StsClient($cred, "ap-beijing");// 实例化一个请求对象,每个接口都会对应一个request对象$req = new GetFederationTokenRequest();$params = array("Name" => "soe","Policy" => "{\\"version\\": \\"2.0\\",\\"statement\\": {\\"effect\\": \\"allow\\", \\"action\\":[\\"soe:TransmitOralProcessWithInit\\"],\\"resource\\": \\"*\\"}}");$req->fromJsonString(json_encode($params));// 返回的resp是一个GetFederationTokenResponse的实例,与请求对象对应$resp = $client->GetFederationToken($req);// 输出json格式的字符串回包print_r($resp->toJsonString());} catch (TencentCloudSDKException $e) {echo $e;}