全部产品
云市场

RESTful API 2.0

更新时间:2019-11-07 11:35:21

功能介绍

一句话识别RESTful API支持以POST方式整段上传不超过一分钟的语音文件。识别结果将以JSON格式在请求响应中一次性返回,开发者需要保证在识别结果返回之前连接不被中断。

  • 支持音频编码格式:pcm(无压缩的pcm文件或wav文件)、opus,16bit采样位数的单声道(mono);
  • 支持音频采样率:8000Hz、16000Hz;
  • 支持对返回结果进行设置:是否在后处理中添加标点,是否将中文数字转为阿拉伯数字输出;
  • 支持控制台配置项目热词和自学习模型训练;
  • 支持多种语言的识别,可在控制台编辑项目进行模型配置。

交互流程

客户端向服务端发送带有音频数据的HTTP REST POST请求,服务端返回带有识别结果的HTTP响应。

RESTful

说明:所有服务端的响应都会在返回信息中包含task_id参数,用于表示本次识别任务的ID,请记录下这个值,如果发生错误,请将task_id和错误信息提交到工单。

服务地址

访问类型 说明 URL Host
外网访问 所有服务器均可使用外网访问URL http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr nls-gateway.cn-shanghai.aliyuncs.com
阿里云上海ECS内网访问 您使用阿里云上海ECS(即ECS地域为华东2(上海)),可使用内网访问URL
说明:使用内网访问方式,将不产生ECS实例的公网流量费用。
ECS的经典网络不能访问AnyTunnel,即不能在内网访问语音服务;如果希望使用AnyTunnel,需要创建专有网络后在其内部访问。
http://nls-gateway.cn-shanghai-internal.aliyuncs.com/stream/v1/asr nls-gateway.cn-shanghai-internal.aliyuncs.com

以下将以使用外网访问URL的方式进行介绍。如果您使用的是阿里云上海ECS,并想使用内网访问URL,直接替换外网访问URL和其对用的Host即可。

上传音频文件

一句话识别请求HTTP报文实例:

  1. POST /stream/v1/asr?appkey=23****f5&format=pcm&sample_rate=16000&enable_punctuation_prediction=true&enable_inverse_text_normalization=true HTTP/1.1
  2. X-NLS-Token: 450372e4279******bcc2b3c793
  3. Content-type: application/octet-stream
  4. Content-Length: 94616
  5. Host: nls-gateway.cn-shanghai.aliyuncs.com
  6. [audio data]

一个完整的一句话识别RESTful API请求需包含以下要素:

HTTP 请求行

HTTP的请求行指定了URL和请求参数。

URL

协议 URL 方法
HTTP/1.1 http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr POST

请求参数

Parameter Type Description
appkey String 应用appkey,必填(获取方法请阅读创建项目一节)
format String 音频编码格式,可选,支持的格式:pcm、opus,默认是pcm
sample_rate Integer 音频采样率,可选,16000Hz或者8000Hz,默认是16000Hz
vocabulary_id String 指定添加热词表ID,可选,默认不添加
customization_id String 指定添加自学习模型ID,可选,默认不添加
enable_punctuation_prediction Boolean 是否在后处理中添加标点,可选,true或者false,默认false不开启
enable_inverse_text_normalization Boolean 是否在后处理中执行ITN,可选,true或者false,默认false不开启
enable_voice_detection Boolean 是否启动语音检测,可选,true或者false,默认false不开启。说明:如果开启语音检测,服务端会对上传的音频进行静音检测,切除静音部分和之后的语音内容,不再对其进行识别;不同的模型表现结果不同。

如上URL和请求参数组成的完整请求链接为:

  1. http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr?appkey=Yu1******uncS&format=pcm&sample_rate=16000&vocabulary_id=a17******d6b&customization_id=abd******ed8&enable_punctuation_prediction=true&enable_inverse_text_normalization=true&enable_voice_detection=true

HTTP 请求头部

HTTP 请求头部由“关键字/值”对组成,每行一对,关键字和值用英文冒号“:”分隔,设置内容为如下表格:

名称 类型 需求 描述
X-NLS-Token String 必填 服务鉴权Token,获取方法请阅读获取 Token一节
Content-type String 必填 必须为“application/octet-stream”,表明HTTP body的数据为二进制流
Content-Length long 必填 HTTP body中请求数据的长度,即音频文件的长度
Host String 必填 HTTP请求的服务器域名,必须为“nls-gateway.cn-shanghai.aliyuncs.com”

HTTP 请求体

HTTP请求体传入的是二进制音频数据,因此在HTTP请求头部中的Content-Type必须设置为application/octet-stream。

响应结果

发送上传音频的HTTP请求之后,会收到服务端的响应,识别的结果以JSON字符串的形式保存在该响应中。

成功响应

  1. {
  2. "task_id": "cf7b0c5339244ee29cd4e43fb97fd52e",
  3. "result": "北京的天气。",
  4. "status":20000000,
  5. "message":"SUCCESS"
  6. }

失败响应

以鉴权token错误为例:

  1. {
  2. "task_id": "8bae3613dfc54ebfa811a17d8a7a9ae7",
  3. "result": "",
  4. "status": 40000001,
  5. "message": "Gateway:ACCESS_DENIED:The token 'c0c1e860f3*******de8091c68a' is invalid!"
  6. }

响应字段说明

Parameter Type Description
task_id String 32位任务ID,请记录该值,以便于排查错误
result String 语音识别结果
status Integer 服务状态码
message String 服务状态描述

服务状态码说明

20000000表示成功,4开头的状态码表示客户端的错误,5开头的错误码表示服务端的错误。

服务状态码 服务状态描述 解决方案
20000000 请求成功
40000000 默认的客户端错误码 查看错误消息或提交工单
40000001 身份认证失败 检查使用的令牌是否正确,是否过期
40000002 无效的消息 检查发送的消息是否符合要求
40000003 无效的参数 检查参数值设置是否合理
40000004 空闲超时 确认是否长时间没有发送数据掉服务端
40000005 请求数量过多 检查是否超过了并发连接数或者每秒钟请求数
50000000 默认的服务端错误 如果偶现可以忽略,重复出现请提交工单
50000001 内部GRPC调用错误 如果偶现可以忽略,重复出现请提交工单

快速测试

音频文件下载链接:nls-sample-16k.wav

使用cURL命令行可以快速进行一句话识别的RESTful API测试。

  1. curl -X POST -H "X-NLS-Token: ${token}" http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr?appkey=${appkey} --data-binary @${audio_file}
  2. 示例:
  3. curl -X POST -H "X-NLS-Token: 4a036*******531cf" http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr?appkey=tt4******3P2u --data-binary @./nls-sample-16k.wav

音频文件说明:音频文件下载链接:nls-sample-16k.wav,模型为通用模型;若使用其他音频文件,请填入对应的编码格式和采样率,并在管控台设置对应的模型,模型设置请阅读管理项目一节。

Java Demo

依赖:

  1. <dependency>
  2. <groupId>com.squareup.okhttp3</groupId>
  3. <artifactId>okhttp</artifactId>
  4. <version>3.9.1</version>
  5. </dependency>
  6. <!-- http://mvnrepository.com/artifact/com.alibaba/fastjson -->
  7. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>fastjson</artifactId>
  10. <version>1.2.42</version>
  11. </dependency>

发送请求与响应:

  1. import com.alibaba.fastjson.JSONPath;
  2. import com.alibaba.nls.client.example.utils.HttpUtil;
  3. import java.util.HashMap;
  4. public class SpeechRecognizerRESTfulDemo {
  5. private String accessToken;
  6. private String appkey;
  7. public SpeechRecognizerRESTfulDemo(String appkey, String token) {
  8. this.appkey = appkey;
  9. this.accessToken = token;
  10. }
  11. public void process(String fileName, String format, int sampleRate,
  12. boolean enablePunctuationPrediction,
  13. boolean enableInverseTextNormalization,
  14. boolean enableVoiceDetection) {
  15. /**
  16. * 设置HTTP REST POST请求
  17. * 1.使用http协议
  18. * 2.语音识别服务域名:nls-gateway.cn-shanghai.aliyuncs.com
  19. * 3.语音识别接口请求路径:/stream/v1/asr
  20. * 4.设置必须请求参数:appkey、format、sample_rate,
  21. * 5.设置可选请求参数:enable_punctuation_prediction、enable_inverse_text_normalization、enable_voice_detection
  22. */
  23. String url = "http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr";
  24. String request = url;
  25. request = request + "?appkey=" + appkey;
  26. request = request + "&format=" + format;
  27. request = request + "&sample_rate=" + sampleRate;
  28. if (enablePunctuationPrediction) {
  29. request = request + "&enable_punctuation_prediction=" + true;
  30. }
  31. if (enableInverseTextNormalization) {
  32. request = request + "&enable_inverse_text_normalization=" + true;
  33. }
  34. if (enableVoiceDetection) {
  35. request = request + "&enable_voice_detection=" + true;
  36. }
  37. System.out.println("Request: " + request);
  38. /**
  39. * 设置HTTP 头部字段
  40. * 1.鉴权参数
  41. * 2.Content-Type:application/octet-stream
  42. */
  43. HashMap<String, String> headers = new HashMap<String, String>();
  44. headers.put("X-NLS-Token", this.accessToken);
  45. headers.put("Content-Type", "application/octet-stream");
  46. /**
  47. * 发送HTTP POST请求,返回服务端的响应
  48. */
  49. String response = HttpUtil.sendPostFile(request, headers, fileName);
  50. if (response != null) {
  51. System.out.println("Response: " + response);
  52. String result = JSONPath.read(response, "result").toString();
  53. System.out.println("识别结果:" + result);
  54. }
  55. else {
  56. System.err.println("识别失败!");
  57. }
  58. }
  59. public static void main(String[] args) {
  60. if (args.length < 2) {
  61. System.err.println("SpeechRecognizerRESTfulDemo need params: <token> <app-key>");
  62. System.exit(-1);
  63. }
  64. String token = args[0];
  65. String appkey = args[1];
  66. SpeechRecognizerRESTfulDemo demo = new SpeechRecognizerRESTfulDemo(appkey, token);
  67. String fileName = SpeechRecognizerRESTfulDemo.class.getClassLoader().getResource("./nls-sample-16k.wav").getPath();
  68. String format = "pcm";
  69. int sampleRate = 16000;
  70. boolean enablePunctuationPrediction = true;
  71. boolean enableInverseTextNormalization = true;
  72. boolean enableVoiceDetection = false;
  73. demo.process(fileName, format, sampleRate, enablePunctuationPrediction, enableInverseTextNormalization, enableVoiceDetection);
  74. }
  75. }

HttpUtils 类:

  1. import okhttp3.*;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.net.SocketTimeoutException;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.concurrent.TimeUnit;
  8. public class HttpUtil {
  9. private static String getResponseWithTimeout(Request q) {
  10. String ret = null;
  11. OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
  12. OkHttpClient client = httpBuilder.connectTimeout(10, TimeUnit.SECONDS)
  13. .readTimeout(60, TimeUnit.SECONDS)
  14. .writeTimeout(60, TimeUnit.SECONDS)
  15. .build();
  16. try {
  17. Response s = client.newCall(q).execute();
  18. ret = s.body().string();
  19. s.close();
  20. } catch (SocketTimeoutException e) {
  21. ret = null;
  22. System.err.println("get result timeout");
  23. } catch (IOException e) {
  24. System.err.println("get result error " + e.getMessage());
  25. }
  26. return ret;
  27. }
  28. public static String sendPostFile(String url, HashMap<String, String> headers, String fileName) {
  29. RequestBody body;
  30. File file = new File(fileName);
  31. if (!file.isFile()) {
  32. System.err.println("The filePath is not a file: " + fileName);
  33. return null;
  34. } else {
  35. body = RequestBody.create(MediaType.parse("application/octet-stream"), file);
  36. }
  37. Headers.Builder hb = new Headers.Builder();
  38. if (headers != null && !headers.isEmpty()) {
  39. for (Map.Entry<String, String> entry : headers.entrySet()) {
  40. hb.add(entry.getKey(), entry.getValue());
  41. }
  42. }
  43. Request request = new Request.Builder()
  44. .url(url)
  45. .headers(hb.build())
  46. .post(body)
  47. .build();
  48. return getResponseWithTimeout(request);
  49. }
  50. public static String sendPostData(String url, HashMap<String, String> headers, byte[] data) {
  51. RequestBody body;
  52. if (data.length == 0) {
  53. System.err.println("The send data is empty.");
  54. return null;
  55. } else {
  56. body = RequestBody.create(MediaType.parse("application/octet-stream"), data);
  57. }
  58. Headers.Builder hb = new Headers.Builder();
  59. if (headers != null && !headers.isEmpty()) {
  60. for (Map.Entry<String, String> entry : headers.entrySet()) {
  61. hb.add(entry.getKey(), entry.getValue());
  62. }
  63. }
  64. Request request = new Request.Builder()
  65. .url(url)
  66. .headers(hb.build())
  67. .post(body)
  68. .build();
  69. return getResponseWithTimeout(request);
  70. }
  71. }

C++ Demo

C++ Demo使用了第三方函数库curl处理HTTP的请求和响应。curl库和Demo文件下载链接

目录说明:

  • CMakeLists.txt demo工程的CMakeList文件;
  • demo
文件名 描述
restfulAsrDemo.cpp 一句话RESTful API Demo
  • include
文件名 描述
curl curl库头文件目录
  • lib包含curl动态库,版本为curl-7.60。根据平台不同,可以选择linux版本(glibc:2.5及以上, Gcc4, Gcc5)、windows版本(VS2013、VS2015)。
  • readme.txt 说明
  • release.log 更新记录
  • version 版本号
  • build.sh demo编译脚本

注意:

  1. Linux环境下,运行环境最低要求:Glibc 2.5及以上, Gcc4、Gcc5。
  2. Windows下需要您自己搭建demo工程。
  3. C++ demo的下载包里自带了测试音频sample.pcm。

编译运行:

  1. 1. 请确认本地系统以安装Cmake,最低版本2.4
  2. 2. cd path/to/sdk/lib
  3. 3. tar -zxvpf linux.tar.gz
  4. 4. cd path/to/sdk
  5. 5. 执行[./build.sh]编译demo
  6. 6. 编译完毕,进入demo目录,执行[./restfulAsrDemo]
  7. 如果不支持cmake,可以尝试手动编译:
  8. 1: cd path/to/sdk/lib
  9. 2: tar -zxvpf linux.tar.gz
  10. 3: cd path/to/sdk/demo
  11. 4: g++ -o restfulAsrDemo restfulAsrDemo.cpp -I path/to/sdk/include -L path/to/sdk/lib/linux -lssl -lcrypto -lcurl -D_GLIBCXX_USE_CXX11_ABI=0
  12. 5: export LD_LIBRARY_PATH=path/to/sdk/lib/linux/
  13. 6: ./restfulAsrDemo your-token your-appkey
  14. Windows平台需要您自己搭建工程。

示例代码:

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include "curl/curl.h"
  6. using namespace std;
  7. #ifdef _WIN32
  8. string UTF8ToGBK(const string& strUTF8) {
  9. int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
  10. unsigned short * wszGBK = new unsigned short[len + 1];
  11. memset(wszGBK, 0, len * 2 + 2);
  12. MultiByteToWideChar(CP_UTF8, 0, (char*)strUTF8.c_str(), -1, (wchar_t*)wszGBK, len);
  13. len = WideCharToMultiByte(CP_ACP, 0, (wchar_t*)wszGBK, -1, NULL, 0, NULL, NULL);
  14. char *szGBK = new char[len + 1];
  15. memset(szGBK, 0, len + 1);
  16. WideCharToMultiByte(CP_ACP, 0, (wchar_t*)wszGBK, -1, szGBK, len, NULL, NULL);
  17. string strTemp(szGBK);
  18. delete[] szGBK;
  19. delete[] wszGBK;
  20. return strTemp;
  21. }
  22. #endif
  23. /**
  24. * 一句话识别RESTful API HTTP请求的响应回调函数
  25. * 识别结果为JSON格式的字符串
  26. */
  27. size_t responseCallback(void* ptr, size_t size, size_t nmemb, void* userData) {
  28. string* srResult = (string*)userData;
  29. size_t len = size * nmemb;
  30. char *pBuf = (char*)ptr;
  31. string response = string(pBuf, pBuf + len);
  32. #ifdef _WIN32
  33. response = UTF8ToGBK(response);
  34. #endif
  35. cout << "current result: " << response << endl;
  36. *srResult += response;
  37. cout << "total result: " << *srResult << endl;
  38. return len;
  39. }
  40. int sendAsrRequest(const char* request, const char* token, const char* fileName, string* srResult) {
  41. CURL* curl = NULL;
  42. CURLcode res;
  43. /**
  44. * 读取音频文件
  45. */
  46. ifstream fs;
  47. fs.open(fileName, ios::out | ios::binary);
  48. if (!fs.is_open()) {
  49. cerr << "The audio file is not exist!" << endl;
  50. return -1;
  51. }
  52. stringstream buffer;
  53. buffer << fs.rdbuf();
  54. string audioData(buffer.str());
  55. curl = curl_easy_init();
  56. if (curl == NULL) {
  57. return -1;
  58. }
  59. /**
  60. * 设置HTTP请求行
  61. */
  62. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  63. curl_easy_setopt(curl, CURLOPT_URL, request);
  64. /**
  65. * 设置HTTP请求头部
  66. */
  67. struct curl_slist* headers = NULL;
  68. // token
  69. string X_NLS_Token = "X-NLS-Token:";
  70. X_NLS_Token += token;
  71. headers = curl_slist_append(headers, X_NLS_Token.c_str());
  72. // Content-Type
  73. headers = curl_slist_append(headers, "Content-Type:application/octet-stream");
  74. // Content-Length
  75. string content_Length = "Content-Length:";
  76. ostringstream oss;
  77. oss << content_Length << audioData.length();
  78. content_Length = oss.str();
  79. headers = curl_slist_append(headers, content_Length.c_str());
  80. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  81. /**
  82. * 设置HTTP请求数据
  83. */
  84. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, audioData.c_str());
  85. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, audioData.length());
  86. /**
  87. * 设置HTTP请求的响应回调函数
  88. */
  89. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, responseCallback);
  90. curl_easy_setopt(curl, CURLOPT_WRITEDATA, srResult);
  91. /**
  92. * 发送HTTP请求
  93. */
  94. res = curl_easy_perform(curl);
  95. // 释放资源
  96. curl_slist_free_all(headers);
  97. curl_easy_cleanup(curl);
  98. if (res != CURLE_OK) {
  99. cerr << "curl_easy_perform failed: " << curl_easy_strerror(res) << endl;
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. int process(const char* request, const char* token, const char* fileName) {
  105. // 全局只初始化一次
  106. curl_global_init(CURL_GLOBAL_ALL);
  107. string srResult = "";
  108. int ret = sendAsrRequest(request, token, fileName, &srResult);
  109. curl_global_cleanup();
  110. return ret;
  111. }
  112. int main(int argc, char* argv[]) {
  113. if (argc < 3) {
  114. cerr << "params is not valid. Usage: ./demo your_token your_appkey" << endl;
  115. return -1;
  116. }
  117. string token = argv[1];
  118. string appKey = argv[2];
  119. string url = "http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr";
  120. string format = "pcm";
  121. int sampleRate = 16000;
  122. bool enablePunctuationPrediction = true;
  123. bool enableInverseTextNormalization = true;
  124. bool enableVoiceDetection = false;
  125. string fileName = "sample.pcm";
  126. /**
  127. * 设置RESTful请求参数
  128. */
  129. ostringstream oss;
  130. oss << url;
  131. oss << "?appkey=" << appKey;
  132. oss << "&format=" << format;
  133. oss << "&sample_rate=" << sampleRate;
  134. if (enablePunctuationPrediction) {
  135. oss << "&enable_punctuation_prediction=" << "true";
  136. }
  137. if (enableInverseTextNormalization) {
  138. oss << "&enable_inverse_text_normalization=" << "true";
  139. }
  140. if (enableVoiceDetection) {
  141. oss << "&enable_voice_detection=" << "true";
  142. }
  143. string request = oss.str();
  144. cout << "request: " << request << endl;
  145. process(request.c_str(), token.c_str(), fileName.c_str());
  146. return 0;
  147. }

Python Demo

注意:Python 2.x请使用httplib模块;Python 3.x请使用http.client模块。

  1. # -*- coding: UTF-8 -*-
  2. # Python 2.x 引入httplib模块
  3. # import httplib
  4. # Python 3.x 引入http.client模块
  5. import http.client
  6. import json
  7. def process(request, token, audioFile) :
  8. # 读取音频文件
  9. with open(audioFile, mode = 'rb') as f:
  10. audioContent = f.read()
  11. host = 'nls-gateway.cn-shanghai.aliyuncs.com'
  12. # 设置HTTP请求头部
  13. httpHeaders = {
  14. 'X-NLS-Token': token,
  15. 'Content-type': 'application/octet-stream',
  16. 'Content-Length': len(audioContent)
  17. }
  18. # Python 2.x 请使用httplib
  19. # conn = httplib.HTTPConnection(host)
  20. # Python 3.x 请使用http.client
  21. conn = http.client.HTTPConnection(host)
  22. conn.request(method='POST', url=request, body=audioContent, headers=httpHeaders)
  23. response = conn.getresponse()
  24. print('Response status and response reason:')
  25. print(response.status ,response.reason)
  26. body = response.read()
  27. try:
  28. print('Recognize response is:')
  29. body = json.loads(body)
  30. print(body)
  31. status = body['status']
  32. if status == 20000000 :
  33. result = body['result']
  34. print('Recognize result: ' + result)
  35. else :
  36. print('Recognizer failed!')
  37. except ValueError:
  38. print('The response is not json format string')
  39. conn.close()
  40. appKey = '您的app_key'
  41. token = '您的服务鉴权Token'
  42. # 服务请求地址
  43. url = 'http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr'
  44. # 音频文件
  45. audioFile = '/path/to/nls-sample-16k.wav'
  46. format = 'pcm'
  47. sampleRate = 16000
  48. enablePunctuationPrediction = True
  49. enableInverseTextNormalization = True
  50. enableVoiceDetection = False
  51. # 设置RESTful请求参数
  52. request = url + '?appkey=' + appKey
  53. request = request + '&format=' + format
  54. request = request + '&sample_rate=' + str(sampleRate)
  55. if enablePunctuationPrediction :
  56. request = request + '&enable_punctuation_prediction=' + 'true'
  57. if enableInverseTextNormalization :
  58. request = request + '&enable_inverse_text_normalization=' + 'true'
  59. if enableVoiceDetection :
  60. request = request + '&enable_voice_detection=' + 'true'
  61. print('Request: ' + request)
  62. process(request, token, audioFile)

PHP Demo

说明:PHP Demo中使用了cURL函数,要求PHP的版本在4.0.2以上,并且确保安装了cURL扩展。

  1. <?php
  2. function process($token, $request, $audioFile) {
  3. /**
  4. * 读取音频文件
  5. */
  6. $audioContent = file_get_contents($audioFile);
  7. if ($audioContent == FALSE) {
  8. print "The audio file is not exist!\n";
  9. return;
  10. }
  11. $curl = curl_init();
  12. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  13. curl_setopt($curl, CURLOPT_TIMEOUT, 120);
  14. /**
  15. * 设置HTTP请求行
  16. */
  17. curl_setopt($curl, CURLOPT_URL, $request);
  18. curl_setopt($curl, CURLOPT_POST,TRUE);
  19. /**
  20. * 设置HTTP请求头部
  21. */
  22. $contentType = "application/octet-stream";
  23. $contentLength = strlen($audioContent);
  24. $headers = array(
  25. "X-NLS-Token:" . $token,
  26. "Content-type:" . $contentType,
  27. "Content-Length:" . strval($contentLength)
  28. );
  29. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  30. /**
  31. * 设置HTTP请求数据
  32. */
  33. curl_setopt($curl, CURLOPT_POSTFIELDS, $audioContent);
  34. curl_setopt($curl, CURLOPT_NOBODY, FALSE);
  35. /**
  36. * 发送HTTP请求
  37. */
  38. $returnData = curl_exec($curl);
  39. curl_close($curl);
  40. if ($returnData == FALSE) {
  41. print "curl_exec failed!\n";
  42. return;
  43. }
  44. print $returnData . "\n";
  45. $resultArr = json_decode($returnData, true);
  46. $status = $resultArr["status"];
  47. if ($status == 20000000) {
  48. $result = $resultArr["result"];
  49. print "The audio file recognized result: " . $result . "\n";
  50. }
  51. else {
  52. print "The audio file recognized failed.\n";
  53. }
  54. }
  55. $appkey = "您的app_key";
  56. $token = "您的服务鉴权Token";
  57. $url = "http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr";
  58. $audioFile = "/path/to/nls-sample-16k.wav";
  59. $format = "pcm";
  60. $sampleRate = 16000;
  61. $enablePunctuationPrediction = TRUE;
  62. $enableInverseTextNormalization = TRUE;
  63. $enableVoiceDetection = FALSE;
  64. /**
  65. * 设置RESTful 请求参数
  66. */
  67. $request = $url;
  68. $request = $request . "?appkey=" . $appkey;
  69. $request = $request . "&format=" . $format;
  70. $request = $request . "&sample_rate=" . strval($sampleRate);
  71. if ($enablePunctuationPrediction) {
  72. $request = $request . "&enable_punctuation_prediction=" . "true";
  73. }
  74. if ($enableInverseTextNormalization) {
  75. $request = $request . "&enable_inverse_text_normalization=" . "true";
  76. }
  77. if ($enableVoiceDetection) {
  78. $request = $request . "&enable_voice_detection=" . "true";
  79. }
  80. print "Request: " . $request . "\n";
  81. process($token, $request, $audioFile);
  82. ?>

Node.js Demo

说明:request依赖安装,请在您的Demo文件所在目录执行如下命令:

  1. npm install request --save

代码示例:

  1. const request = require('request');
  2. const fs = require('fs');
  3. function callback(error, response, body) {
  4. if (error != null) {
  5. console.log(error);
  6. }
  7. else {
  8. console.log('The audio file recognized result:');
  9. console.log(body);
  10. if (response.statusCode == 200) {
  11. body = JSON.parse(body);
  12. if (body.status == 20000000) {
  13. console.log('result: ' + body.result);
  14. console.log('The audio file recognized succeed!');
  15. } else {
  16. console.log('The audio file recognized failed!');
  17. }
  18. } else {
  19. console.log('The audio file recognized failed, http code: ' + response.statusCode);
  20. }
  21. }
  22. }
  23. function process(requestUrl, token, audioFile) {
  24. /**
  25. * 读取音频文件
  26. */
  27. var audioContent = null;
  28. try {
  29. audioContent = fs.readFileSync(audioFile);
  30. } catch(error) {
  31. if (error.code == 'ENOENT') {
  32. console.log('The audio file is not exist!');
  33. }
  34. return;
  35. }
  36. /**
  37. * 设置HTTP 请求头部
  38. */
  39. var httpHeaders = {
  40. 'X-NLS-Token': token,
  41. 'Content-type': 'application/octet-stream',
  42. 'Content-Length': audioContent.length
  43. };
  44. var options = {
  45. url: requestUrl,
  46. method: 'POST',
  47. headers: httpHeaders,
  48. body: audioContent
  49. };
  50. request(options, callback);
  51. }
  52. var appkey = '您的app_key';
  53. var token = '您的服务鉴权Token';
  54. var url = 'http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr';
  55. var audioFile = '/path/to/nls-sample-16k.wav';
  56. var format = 'pcm';
  57. var sampleRate = '16000';
  58. var enablePunctuationPrediction = true;
  59. var enableInverseTextNormalization = true;
  60. var enableVoiceDetection = false;
  61. /**
  62. * 设置RESTful 请求参数
  63. */
  64. var requestUrl = url;
  65. requestUrl = requestUrl + '?appkey=' + appkey;
  66. requestUrl = requestUrl + '&format=' + format;
  67. requestUrl = requestUrl + '&sample_rate=' + sampleRate;
  68. if (enablePunctuationPrediction) {
  69. requestUrl = requestUrl + '&enable_punctuation_prediction=' + 'true';
  70. }
  71. if (enableInverseTextNormalization) {
  72. requestUrl = requestUrl + '&enable_inverse_text_normalization=' + 'true';
  73. }
  74. if (enableVoiceDetection) {
  75. requestUrl = requestUrl + '&enable_voice_detection=' + 'true';
  76. }
  77. process(requestUrl, token, audioFile);

.Net Demo

说明:Demo依赖了System.Net.Http、Newtonsoft.Json.Linq

  1. using System;
  2. using System.Net.Http;
  3. using System.IO;
  4. using Newtonsoft.Json.Linq;
  5. namespace RESTfulAPI
  6. {
  7. class SpeechRecognizerRESTfulDemo
  8. {
  9. private string token;
  10. private string appkey;
  11. public SpeechRecognizerRESTfulDemo(string appkey, string token)
  12. {
  13. this.appkey = appkey;
  14. this.token = token;
  15. }
  16. public void process(string fileName, string format, int sampleRate,
  17. bool enablePunctuationPrediction,
  18. bool enableInverseTextNormalization,
  19. bool enableVoiceDetection)
  20. {
  21. /**
  22. * 设置HTTP REST POST请求
  23. * 1.使用http协议
  24. * 2.语音识别服务域名:nls-gateway.cn-shanghai.aliyuncs.com
  25. * 3.语音识别接口请求路径:/stream/v1/asr
  26. * 4.设置必须请求参数:appkey、format、sample_rate,
  27. * 5.设置可选请求参数:enable_punctuation_prediction、enable_inverse_text_normalization、enable_voice_detection
  28. */
  29. string url = "http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr";
  30. url = url + "?appkey=" + appkey;
  31. url = url + "&format=" + format;
  32. url = url + "&sample_rate=" + sampleRate;
  33. if (enablePunctuationPrediction)
  34. {
  35. url = url + "&enable_punctuation_prediction=" + true;
  36. }
  37. if (enableInverseTextNormalization)
  38. {
  39. url = url + "&enable_inverse_text_normalization=" + true;
  40. }
  41. if (enableVoiceDetection)
  42. {
  43. url = url + "&enable_voice_detection=" + true;
  44. }
  45. System.Console.WriteLine("URL: " + url);
  46. HttpClient client = new HttpClient();
  47. /**
  48. * 设置HTTP 头部字段
  49. * 鉴权参数
  50. */
  51. client.DefaultRequestHeaders.Add("X-NLS-Token", token);
  52. if (!File.Exists(fileName))
  53. {
  54. System.Console.WriteLine("The audio file dose not exist");
  55. return;
  56. }
  57. byte[] audioData = File.ReadAllBytes(fileName);
  58. /**
  59. * 设置HTTP Body
  60. * 音频二进制数据
  61. * Content-Type:application/octet-stream
  62. */
  63. ByteArrayContent content = new ByteArrayContent(audioData);
  64. content.Headers.Add("Content-Type", "application/octet-stream");
  65. /**
  66. * 发送HTTP POST请求,处理服务端的响应
  67. */
  68. HttpResponseMessage response = client.PostAsync(url, content).Result;
  69. string responseBodyAsText = response.Content.ReadAsStringAsync().Result;
  70. System.Console.WriteLine("Response: " + responseBodyAsText);
  71. if (response.IsSuccessStatusCode)
  72. {
  73. JObject obj = JObject.Parse(responseBodyAsText);
  74. string result = obj["result"].ToString();
  75. System.Console.WriteLine("识别结果: " + result);
  76. }
  77. else
  78. {
  79. System.Console.WriteLine("Response status code and reason phrase: " +
  80. response.StatusCode + " " + response.ReasonPhrase);
  81. System.Console.WriteLine("识别失败!");
  82. }
  83. }
  84. static void Main(string[] args)
  85. {
  86. if (args.Length < 2)
  87. {
  88. System.Console.WriteLine("SpeechRecognizerRESTfulDemo need params: <token> <app-key>");
  89. return;
  90. }
  91. string token = args[0];
  92. string appkey = args[1];
  93. SpeechRecognizerRESTfulDemo demo = new SpeechRecognizerRESTfulDemo(appkey, token);
  94. string fileName = "nls-sample-16k.wav";
  95. string format = "pcm";
  96. int sampleRate = 16000;
  97. bool enablePunctuationPrediction = true;
  98. bool enableInverseTextNormalization = true;
  99. bool enableVoiceDetection = false;
  100. demo.process(fileName, format, sampleRate,
  101. enablePunctuationPrediction, enableInverseTextNormalization, enableVoiceDetection);
  102. }
  103. }
  104. }

GO Demo

  1. package main
  2. import (
  3. "fmt"
  4. "encoding/json"
  5. "net/http"
  6. "io/ioutil"
  7. "strconv"
  8. "bytes"
  9. )
  10. func process(appkey string, token string, fileName string, format string, sampleRate int,
  11. enablePunctuationPrediction bool, enableInverseTextNormalization bool, enableVoiceDetection bool) {
  12. /**
  13. * 设置HTTP REST POST请求
  14. * 1.使用http协议
  15. * 2.语音识别服务域名:nls-gateway.cn-shanghai.aliyuncs.com
  16. * 3.语音识别接口请求路径:/stream/v1/asr
  17. * 4.设置必须请求参数:appkey、format、sample_rate,
  18. * 5.设置可选请求参数:enable_punctuation_prediction、enable_inverse_text_normalization、enable_voice_detection
  19. */
  20. var url string = "http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr"
  21. url = url + "?appkey=" + appkey
  22. url = url + "&format=" + format
  23. url = url + "&sample_rate=" + strconv.Itoa(sampleRate)
  24. if (enablePunctuationPrediction) {
  25. url = url + "&enable_punctuation_prediction=" + "true"
  26. }
  27. if (enableInverseTextNormalization) {
  28. url = url + "&enable_inverse_text_normalization=" + "true"
  29. }
  30. if (enableVoiceDetection) {
  31. url = url + "&enable_voice_detection=" + "false"
  32. }
  33. fmt.Println(url)
  34. /**
  35. * 读取音频数据,作为HTTP Body
  36. */
  37. audioData, err := ioutil.ReadFile(fileName)
  38. if err != nil {
  39. panic(err)
  40. }
  41. request, err := http.NewRequest("POST", url, bytes.NewBuffer(audioData))
  42. if err != nil {
  43. panic(err)
  44. }
  45. /**
  46. * 设置HTTP 头部字段
  47. * 1.鉴权参数
  48. * 2.Content-Type:application/octet-stream
  49. */
  50. request.Header.Add("X-NLS-Token", token)
  51. request.Header.Add("Content-Type", "application/octet-stream")
  52. /**
  53. * 发送HTTP POST请求,处理服务端的响应
  54. */
  55. client := &http.Client{}
  56. response, err := client.Do(request)
  57. if err != nil {
  58. panic(err)
  59. }
  60. defer response.Body.Close()
  61. body, _ := ioutil.ReadAll(response.Body)
  62. fmt.Println(string(body))
  63. statusCode := response.StatusCode
  64. if (statusCode == 200) {
  65. var resultMap map[string]interface{}
  66. err = json.Unmarshal([]byte(body), &resultMap)
  67. if err != nil {
  68. panic(err)
  69. }
  70. var result string = resultMap["result"].(string)
  71. fmt.Println("识别成功:" + result)
  72. } else {
  73. fmt.Println("识别失败,HTTP StatusCode: " + strconv.Itoa(statusCode))
  74. }
  75. }
  76. func main() {
  77. var appkey string = "您的appkey"
  78. var token string = "您的token"
  79. var fileName string = "nls-sample-16k.wav"
  80. var format string = "pcm"
  81. var sampleRate int = 16000
  82. var enablePunctuationPrediction bool = true
  83. var enableInverseTextNormalization bool = true
  84. var enableVoiceDetection = false
  85. process(appkey, token, fileName, format, sampleRate, enablePunctuationPrediction, enableInverseTextNormalization, enableVoiceDetection)
  86. }