PHP Demo
本文介绍如何使用阿里云智能语音服务提供的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);
}
}