上传语音文件

POST
https://api.itniotech.com/voice/fileUpload
用户通过该接口上传语音文件,发送语音群呼时通过语音文件id,使用此语 音文件发起群呼,支持.mp3, .m4a和.wav上传。
 
请求参数
参数 说明 是否必填 类型
fileName 带后缀的文件名,5-32字符,名称不允许重复。 String
file base64编码的文件内容(base64编码转换可查看该方法最下方JAVA示例代码) String
 
请求示例
Request URL:
    https://api.itniotech.com/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编码的文件内容"
}
 
响应参数
参数 说明 类型
status 状态码,0成功,其他失败参见响应状态码说明 String
reason 失败原因说明 String
data 语音文件id String
 
响应状态码
status 状态说明
0 成功
-1 账号认证异常
-2 ip限制
-16 时间戳过期
-18 系统异常
-20 数据已存在
-21 数据校验异常
-22 参数异常
 
Java-base64编码转换示例代码:
package com;
import cn.hutool.core.codec.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class fileUpload {
    public static void main(String[] args) {
        File f = new File("c:tmptest.mp3");
        System.out.println(file2Base64(f));
    }

    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;
    }
}
                            
 

LANGUAGE

Java

PHP

REQUEST

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import java.time.LocalDateTime;
import java.time.ZoneId;

void fileUpload() {
    final String baseUrl = "https://api.itniotech.com/voice";
    final String apiKey = "your api key";
    final String apiPwd = "your api secret";
    final String fileName = "";
    final String 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);
    }
} 

REQUEST

$apiKey = "your api key";
$apiSecret = "your api secret";
$url = "https://api.itniotech.com/voice/fileUpload";
$timeStamp = time();
$sign = md5($apiKey.$apiSecret.$timeStamp);

//base64编码的文件内容
$base64Conent = fileToBase64('testAudio.mp3'); //文件的相对路径,此处演示为同级目录下,使用时请酌情修改!

$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);


//文件转base64编码函数
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;
}
 

RESPONSEEXAMPLE

{
    "status": "0",
    "reason": "success",
    "data": "1202202254d4c6372d6f341e999c7ecd0683ee464.mp3"
}