PHP demo

Updated at:

This topic describes how to install and use the PHP software development kit (SDK) for Alibaba Cloud Intelligent Speech Interaction and provides code examples.

Prerequisites

  • Before you use the SDK, read the API reference. For more information, see API reference.

  • Activate Intelligent Speech Interaction and obtain an AccessKey ID and an AccessKey secret. For more information, see Getting started.

Note
  • The PHP examples in this topic use the new Alibaba Cloud SDK for PHP. If you have integrated the old aliyun-openapi-php-sdk, you can continue to use it. We recommend that you update to the new SDK.

  • Future updates to the PHP examples for Audio File Transcription Lite will be based on the new PHP SDK.

SDK description

  • The PHP examples for Audio File Transcription Lite use the Alibaba Cloud PHP SDK to submit requests and query recognition results. These examples call the POP API in RPC style.

  • For more information about the Alibaba Cloud PHP SDK, see PHP SDK.

Important

The Alibaba Cloud PHP SDK requires PHP 5.5.0 or later.

Install the PHP SDK

  1. Install the Alibaba Cloud SDK for PHP. For more information, see Install Alibaba Cloud SDK for PHP.

  2. After you install the Alibaba Cloud SDK for PHP, install the PHP SDK for Audio File Transcription Lite. The composer command is as follows:

    composer require alibabacloud/speechfiletranscriberlite-20211221

Procedure

  1. Create a global client.

  2. Set the request parameters and submit a request for Audio File Transcription Lite. Then, obtain the task ID from the server-side response.

  3. Use the task ID to poll for the recognition result.

Code example

  • Download nls-sample-16k.wav. This audio file is in the PCM format and has a sample rate of 16000 Hz. The general-purpose model is used in this example and is set in the console. If you use a different audio file, you must specify its encoding format and sample rate, and set the corresponding model in the console. For more information about model settings, see Manage projects.

  • Before you call the API, configure the environment variables for your access credentials. The environment variables are ALIYUN_AK_ID for your AccessKey ID, ALIYUN_AK_SECRET for your AccessKey secret, and NLS_APP_KEY for the AppKey of Intelligent Speech Interaction.

<?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');      // To obtain an AccessKey ID and AccessKey secret, go to the console: 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');      // To obtain an AppKey, go to the console: 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);
    /**
     * Step 3: Poll for the recognition result based on the task 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);
   }
}