上傳語音檔案
https://api.laaffic.com/v3/voice/fileUpload
參數 | 説明 | 是否必填 | 類型 |
---|---|---|---|
fileName | 帶後綴的檔名,5-32字符,名稱不允許重複。 | 是 | String |
file | base64 編碼的文件內容(base64編碼轉換可查看該方法最下方JAVA範例代碼) | 是 | String |
Request URL:
https://api.laaffic.com/v3/voice/fileUpload
Request Method:
POST
Request Headers:
Content-Type: application/json;charset=UTF-8
Sign: 05d7a50893e22a5c4bb3216ae3396c7c
Timestamp: 1630468800
Api-Key: bDqJFiq9
Request Body:
{
"fileName":"test.mp3",
"file":"Base64 encoded file content"
}
參數 | 説明 | 類型 |
---|---|---|
status | 狀態碼,0表示成功,其他均爲失敗,詳見狀態碼説明. | String |
reason | 失敗原因説明 | String |
data | 語音檔案id | String |
狀態碼 | 説明 |
---|---|
0 | 成功 |
-1 | 帳號認證異常 |
-2 | IP訪問受限 |
-16 | 超出時間限制 |
-18 | 系統異常 |
-20 | 數據已存在 |
-21 | 數據驗證異常 |
-22 | 參數異常 |
public static String file2Base64(File file) {
if (file == null) {
return null;
}
String base64 = null;
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
byte[] buff = new byte[fin.available()];
fin.read(buff);
base64 = Base64.encode(buff);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}
Java
PHP
請求
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
public void fileUpload() {
final String baseUrl = "https://api.laaffic.com/v3/voice";
final String apiKey = "your api key";
final String apiPwd = "your api secret";
final String fileName = "File name";
final String file = file2Base64(new File(""));
final String url = baseUrl.concat("/fileUpload");
HttpRequest request = HttpRequest.post(url);
// generate md5 key
final String datetime = String.valueOf(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());
final String sign = SecureUtil.md5(apiKey.concat(apiPwd).concat(datetime));
request.header(Header.CONTENT_TYPE, "application/json;charset=UTF-8")
.header("Sign", sign)
.header("Timestamp", datetime)
.header("Api-Key", apiKey);
final String body = JSONUtil.createObj()
.set("fileName", fileName)
.set("file", file)
.toString();
HttpResponse response = request.body(body).execute();
if (response.isOk()) {
String result = response.body();
System.out.println(result);
}
}
請求
header('content-type:text/html;charset=utf8');
$apiKey = "your api key";
$apiSecret = "your api secret";
$url = "https://api.laaffic.com/v3/voice/fileUpload";
$timeStamp = time();
$sign = md5($apiKey.$apiSecret.$timeStamp);
//File content encoded with base64
$base64Conent = fileToBase64('testAudio.mp3'); //The relative path of the file, demonstrated here as in the same level directory, please modify it as appropriate when using it!
$dataArr['fileName'] = 'testAudio.mp3';
$dataArr['file'] = $base64Conent;
$data = json_encode($dataArr);
$headers = array('Content-Type:application/json;charset=UTF-8',"Sign:$sign","Timestamp:$timeStamp","Api-Key:$apiKey");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$output = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
var_dump($output);
//File to base64 encoding function
function fileToBase64($file){
$base64_file = '';
if(file_exists($file)){
$mime_type= mime_content_type($file);
$base64_data = base64_encode(file_get_contents($file));
$base64_file = 'data:'.$mime_type.';base64,'.$base64_data;
}
return $base64_file;
}
響應示例
{
"status": "0",
"reason": "success",
"data": "1202202254d4c6372d6f341e999c7ecd0683ee464.mp3"
}
點擊此處開啟人工服務