本文介紹如何使用阿里云智能語音服務提供的PHP SDK,包括SDK的安裝方法及SDK代碼示例。
前提條件
本文PHP示例基于阿里云新版PHP SDK(Alibaba Cloud SDK for PHP)開發。如果您已接入阿里云舊版PHP SDK(aliyun-openapi-php-sdk),仍然可以繼續使用或者更新到新版SDK(推薦)。
后續錄音文件識別閑時版的PHP示例更新將基于新版PHP SDK。
SDK說明
錄音文件識別閑時版的PHP示例使用了阿里云的PHP SDK提交錄音文件識別閑時版請求和查詢識別結果,采用RPC風格的POP API調用方式。
關于阿里云PHP SDK的詳細介紹請參見PHP SDK。
阿里云PHP SDK適用于PHP 5.5.0或更高版本。
安裝PHP SDK
安裝阿里云PHP SDK,安裝方法詳情請參見安裝Alibaba Cloud SDK for PHP。
安裝完阿里云PHP SDK后,需要安裝錄音文件識別閑時版的PHP SDK,composer命令如下:
composer require alibabacloud/speechfiletranscriberlite-20211221
調用步驟
創建一個全局客戶端。
設置請求參數,提交錄音文件識別閑時版請求;處理服務端返回的響應,獲取任務ID,用于后續的識別結果輪詢。
根據任務ID,輪詢識別結果。
代碼示例
下載nls-sample-16k.wav。該錄音文件為PCM編碼格式16000 Hz采樣率,管控臺設置的模型為通用模型;如果使用其他錄音文件,請填入對應的編碼格式和采樣率,并在管控臺設置對應的模型,模型設置請參見管理項目。
調用接口前,需配置環境變量,通過環境變量讀取訪問憑證。智能語音交互的AccessKey ID、AccessKey Secret和AppKey的環境變量名:ALIYUN_AK_ID、ALIYUN_AK_SECRET、NLS_APP_KEY。
<?php
namespace speechfiletranscriberlite;
require __DIR__ . '/vendor/autoload.php';
use AlibabaCloud\Endpoint\Endpoint;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\SpeechFileTranscriberLite\V20211221\SpeechFileTranscriberLite;
use AlibabaCloud\SDK\SpeechFileTranscriberLite\V20211221\Models\GetTaskResultRequest;
use AlibabaCloud\SDK\SpeechFileTranscriberLite\V20211221\Models\GetTaskResultResponse;
use AlibabaCloud\SDK\SpeechFileTranscriberLite\V20211221\Models\SubmitTaskRequest;
use AlibabaCloud\SDK\SpeechFileTranscriberLite\V20211221\Models\SubmitTaskResponse;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;
use Darabonba\OpenApi\Models\Params;
use Darabonba\OpenApi\OpenApiClient;
$config = new Config();
$config->accessKeyId = getenv('ALIYUN_AK_ID'); //獲取AccessKey ID和AccessKey Secret請前往控制臺:https://ram.console.aliyun.com/manage/ak
$config->accessKeySecret = getenv('ALIYUN_AK_SECRET');
$config->regionId = "cn-shanghai";
$config->endpoint = "speechfiletranscriberlite.cn-shanghai.aliyuncs.com";
$client = new SpeechFileTranscriberLite($config);
$appKey = getenv('NLS_APP_KEY'); //獲取Appkey請前往控制臺:https://nls-portal.console.aliyun.com/applist
$fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav";
$task = array("appkey" => $appKey,
"file_link" => $fileLink,
"version" => "4.0",
"enable_words" => FALSE,
"enable_sample_rate_adaptive" => TRUE);
$taskJson = json_encode($task);
$request = new SubmitTaskRequest();
$request->task = $taskJson;
;$runtime = new RuntimeOptions();
;$runtime->maxIdleConns = 3;
;$runtime->connectTimeout = 10000;
;$runtime->readTimeout = 10000;
$taskId = NULL;
try {
print "taskJson=" . $request->task;
$submitResponse = $client->submitTask($request);
print "response.body.statusCode=" . $submitResponse->body->statusCode;
if (21050000 == $submitResponse->body->statusCode) {
$taskId = $submitResponse->body->taskId;
print "submitTask success,task_id: " . $taskId . "\n";
} else {
print "submitTask failed,statusCode: " . $submitResponse->body->statusCode . ", statusText: " . $submitResponse->body->statusText;
return ;
}
} catch (TeaUnableRetryError $e) {
var_dump($e->getMessage());
var_dump($e->getErrorInfo());
var_dump($e->getLastException());
var_dump($e->getLastRequest());
}
if ($taskId != NULL) {
$request = new GetTaskResultRequest();
$request->taskId = $taskId;
sleep(5);
/**
* 第三步:根據任務 ID 輪詢識別結果。
*/
while (true) {
$getResponse = $client->getTaskResult($request);
if (21050001 == $getResponse->body->statusCode) {
print "getTaskResult: " . $getResponse->body->statusText . "\n";
} else if (21050002 == $getResponse->body->statusCode) {
print "getTaskResult: " . $getResponse->body->statusText . "\n";
} else if (21050000 == $getResponse->body->statusCode) {
print "getTaskResult: " . json_encode($getResponse) . "\n";
return ;
} else {
print "getTaskResult failed,statusCode: " . $getResponse->body->statusCode . ", statusText: " . $getResponse->body->statusText;
return ;
}
sleep(5);
}
}