All Products
Search
Document Center

PHP Demo

Last Updated: May 18, 2020

Note:

  • Read API reference before using the SDK.
  • The SDK in this topic applies only to Intelligent Speech Interaction V2.0 that Alibaba Cloud published on July 5, 2018. If you have activated an earlier version of Intelligent Speech Interaction, activate Intelligent Speech Interaction V2.0 before using the SDK.
  • The PHP demo in this topic is developed based on the latest version of the Alibaba Cloud SDK for PHP. You can directly use the new SDK if you are a new user. If you have integrated an earlier version of the Alibaba Cloud SDK for PHP (aliyun-openapi-php-sdk), you can continue to use this SDK. We recommend that you upgrade it to the new SDK. The PHP demo of the recording file recognition service will be updated based only on the latest version of the Alibaba Cloud SDK for PHP. For more information about the original PHP demo.

SDK instructions

The PHP demo of the recording file recognition service uses the Alibaba Cloud SDK for PHP to send pctowap open platform (POP) API requests in a remote procedure call (RPC) style. You can send a recording file recognition request and query the recording file recognition result. For more information about the Alibaba Cloud SDK for PHP, see Use PHP SDK in PHP SDK Developer Guide.

The Alibaba Cloud SDK for PHP supports PHP 5.5.0 or later.

SDK installation

For more information about how to install the PHP SDK, see the installation guide on GitHub.

1. Download and install Composer.

  • In Linux, run the following command:
  1. curl -sS https://getcomposer.org/installer | php
  • In Windows,click here to download and install Composer.

2. Run Composer commands to install the latest version of the Alibaba Cloud SDK for PHP.

Run the following commands in the directory of demo files. A composer.json file and a vendor folder are generated.

  1. php -d memory_limit=-1 composer.phar require alibabacloud/sdk
  2. php -d memory_limit=-1 composer.phar dump-autoload --optimize

Note: If the composer.phar file is not in the current directory, use an absolute path to reference this file in the preceding commands.

3. Import the autoloader of Composer.

  1. <?php
  2. require __DIR__ . '/vendor/autoload.php';

Note: If demo files and the vendor folder are not in the same directory, use the path of the autoload.php file in the preceding command. For example, run the require '/path/to/vendor/autoload.php'; command.

Call procedure

  1. Create a global client and use the AccessKey ID and AccessKey secret of your Alibaba Cloud account to authenticate the client. For more information about how to obtain your AccessKey information, see Activate Intelligent Speech Interaction in Quick Start.
  2. Create a recording file recognition request and set request parameters. Send the recording file recognition request and process the response returned by the server to obtain the task ID.
  3. Poll the recognition result based on the task ID.

Demo instructions

Note: The recording file in the demo is a pulse-code modulation (PCM) file sampled at 16 kHz. The demo uses a universal model in the console. If you use your own recording file, specify its audio coding format and audio sampling rate and select an appropriate model in the console. For more information about how to select a model, see Manage projects in Console User Guide.

nls-sample-16k.wav


Alibaba Cloud authentication:When using the SDK, you need to use your Alibaba Cloud account to authenticate the client for all API operations. You need to specify the AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information about how to obtain your AccessKey information, see Activate Intelligent Speech Interaction in Quick Start.

Sample code

  1. <?php
  2. require __DIR__ . '/vendor/autoload.php';
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. class NLSFileTrans {
  7. // The keys of request parameters.
  8. private const KEY_APP_KEY = "appkey";
  9. private const KEY_FILE_LINK = "file_link";
  10. private const KEY_VERSION = "version";
  11. private const KEY_ENABLE_WORDS = "enable_words";
  12. // The keys of response parameters.
  13. private const KEY_TASK_ID = "TaskId";
  14. private const KEY_STATUS_TEXT = "StatusText";
  15. private const KEY_RESULT = "Result";
  16. // The status values.
  17. private const STATUS_SUCCESS = "SUCCESS";
  18. private const STATUS_RUNNING = "RUNNING";
  19. private const STATUS_QUEUEING = "QUEUEING";
  20. function submitFileTransRequest($appKey, $fileLink) {
  21. // Set request parameters, including the appkey and file_link parameters. Add them to the request body as a JSON-formatted string.
  22. // Specify the version of the recording file recognition service. If you are a new user, set this parameter to 4.0. If you use the default version 2.0, comment out this parameter.
  23. // Specify whether to return the recognition results of words. Default value: false. This parameter takes effect only when the version of the recording file recognition service is 4.0.
  24. $taskArr = array(self::KEY_APP_KEY => $appKey, self::KEY_FILE_LINK => $fileLink, self::KEY_VERSION => "4.0", self::KEY_ENABLE_WORDS => FALSE);
  25. $task = json_encode($taskArr);
  26. print $task . "\n";
  27. try {
  28. // Send the request and obtain a response from the server.
  29. $submitTaskResponse = AlibabaCloud::nlsFiletrans()
  30. ->v20180817()
  31. ->submitTask()
  32. ->withTask($task)
  33. ->request();
  34. print $submitTaskResponse . "\n";
  35. // Obtain the ID of the recording file recognition task. You can use the task ID to query the recognition result.
  36. $taskId = NULL;
  37. $statusText = $submitTaskResponse[self::KEY_STATUS_TEXT];
  38. if (strcmp(self::STATUS_SUCCESS, $statusText) == 0) {
  39. $taskId = $submitTaskResponse[self::KEY_TASK_ID];
  40. }
  41. return $taskId;
  42. } catch (ClientException $exception) {
  43. // Obtain the error message.
  44. print_r($exception->getErrorMessage());
  45. } catch (ServerException $exception) {
  46. // Obtain the error message.
  47. print_r($exception->getErrorMessage());
  48. }
  49. }
  50. function getFileTransResult($taskId) {
  51. $result = NULL;
  52. while (TRUE) {
  53. try {
  54. $getResultResponse = AlibabaCloud::nlsFiletrans()
  55. ->v20180817()
  56. ->getTaskResult()
  57. ->withTaskId($taskId)
  58. ->request();
  59. print "Queried recording file recognition result: " . $getResultResponse . "\n";
  60. $statusText = $getResultResponse[self::KEY_STATUS_TEXT];
  61. if (strcmp(self::STATUS_RUNNING, $statusText) == 0 || strcmp(self::STATUS_QUEUEING, $statusText) == 0) {
  62. // Continue the polling.
  63. sleep(10);
  64. }
  65. else {
  66. if (strcmp(self::STATUS_SUCCESS, $statusText) == 0) {
  67. $result = $getResultResponse;
  68. }
  69. // End the polling.
  70. break;
  71. }
  72. } catch (ClientException $exception) {
  73. // Obtain the error message.
  74. print_r($exception->getErrorMessage());
  75. } catch (ServerException $exception) {
  76. // Obtain the error message.
  77. print_r($exception->getErrorMessage());
  78. }
  79. }
  80. return $result;
  81. }
  82. }
  83. $accessKeyId = "Your AccessKey ID";
  84. $accessKeySecret = "Your AccessKey secret";
  85. $appKey = "Your appkey";
  86. $fileLink = "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav";
  87. /**
  88. * Step 1: Create a global client.
  89. * Use the AccessKey ID and AccessKey secret of an Alibaba Cloud RAM user to authenticate the client.
  90. */
  91. AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
  92. ->regionId("ap-southeast-1")
  93. ->asGlobalClient();
  94. $fileTrans = new NLSFileTrans();
  95. /**
  96. * Step 2: Send the recording file recognition request and obtain the task ID for subsequent recognition result polling.
  97. */
  98. $taskId = $fileTrans->submitFileTransRequest($appKey, $fileLink);
  99. if ($taskId != NULL) {
  100. print "The recording file recognition request is successful. task_id: " . $taskId . "\n";
  101. }
  102. else {
  103. print "The recording file recognition request fails." ;
  104. return ;
  105. }
  106. /**
  107. * Step 3: Poll the recognition result based on the task ID.
  108. */
  109. $result = $fileTrans->getFileTransResult($taskId);
  110. if ($result != NULL) {
  111. print "Queried recording file recognition result: " . $result . "\n";
  112. }
  113. else {
  114. print "Failed to query the recording file recognition result." ;
  115. }