API概览

本API提供语音合成服务,支持将文本转换为MP3格式的语音文件。

认证信息

所有API请求需要在请求头中包含以下认证信息:

  • X-App-Id: 应用ID
  • X-Timestamp: 当前时间戳(秒)
  • X-Sign: 签名,计算方式:md5(app_id + timestamp + data + app_secret)

签名计算示例

示例数据
app_id = demo_app
app_secret = demo_secret_123
timestamp = 1620000000
data = {"text": "这是一段测试文本", "options": {"rate": 0, "volume": 100}}
                    
计算步骤
  1. 将所有参数按照顺序拼接:demo_app1620000000{"text": "这是一段测试文本", "options": {"rate": 0, "volume": 100}}demo_secret_123
  2. 对拼接后的字符串进行MD5哈希计算
  3. 得到签名:5f4dcc3b5aa765d61d8327deb882cf99
代码示例(PHP)
$app_id = 'demo_app';
$app_secret = 'demo_secret_123';
$timestamp = time();
$data = json_encode(['text' => '这是一段测试文本', 'options' => ['rate' => 0, 'volume' => 100]]);
$sign = md5($app_id . $timestamp . $data . $app_secret);
echo "签名: " . $sign; // 输出: 5f4dcc3b5aa765d61d8327deb882cf99
代码示例(JavaScript)
// 需要引入MD5库,例如:https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.19.0/js/md5.min.js
const app_id = 'demo_app';
const app_secret = 'demo_secret_123';
const timestamp = Math.floor(Date.now() / 1000);
const data = JSON.stringify({text: '这是一段测试文本', options: {rate: 0, volume: 100}});
const sign = md5(app_id + timestamp + data + app_secret);
console.log('签名:', sign); // 输出: 5f4dcc3b5aa765d61d8327deb882cf99

API端点

POST/api/tts/synthesize

将文本转换为语音。

请求参数

参数名 类型 必填 描述
text string 要合成的文本内容,最大长度为500个字符。
options object 合成选项,包含voice、rate、volume、pitch等参数。
callback object 回调信息,包含url、method等参数。

请求示例

cURL示例
# 基本示例
curl -X POST "https://tts.fjyjsw.com/api/tts/synthesize" \
  -H "Content-Type: application/json" \
  -H "X-App-Id: demo_app" \
  -H "X-Timestamp: 1620000000" \
  -H "X-Sign: 5f4dcc3b5aa765d61d8327deb882cf99" \
  -d '{"text": "这是一段测试文本", "options": {"rate": 0, "volume": 100}}'

# 带回调的示例
curl -X POST "https://tts.fjyjsw.com/api/tts/synthesize" \
  -H "Content-Type: application/json" \
  -H "X-App-Id: demo_app" \
  -H "X-Timestamp: 1620000000" \
  -H "X-Sign: 5f4dcc3b5aa765d61d8327deb882cf99" \
  -d '{"text": "这是一段测试文本", "options": {"rate": 0, "volume": 100}, "callback": {"url": "https://your-api.com/callback", "method": "POST"}}'
                    
JavaScript示例
const appId = 'demo_app';
const appSecret = 'demo_secret_123';
const timestamp = Math.floor(Date.now() / 1000);
const data = JSON.stringify({text: "这是一段测试文本", options: {rate: 0, volume: 100}});
const sign = md5(appId + timestamp + data + appSecret);

fetch('https://tts.fjyjsw.com/api/tts/synthesize', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'X-App-Id': appId,
        'X-Timestamp': timestamp,
        'X-Sign': sign
    },
    body: data
})
.then(response => response.json())
.then(result => {
    console.log('合成结果:', result);
})
.catch(error => {
    console.error('错误:', error);
});
                    
PHP示例
$app_id = 'demo_app';
$app_secret = 'demo_secret_123';
$timestamp = time();
$data = json_encode([
    'text' => '这是一段测试文本',
    'options' => ['rate' => 0, 'volume' => 100]
]);

// 计算签名
$sign = md5($app_id . $timestamp . $data . $app_secret);

// API URL
$url = 'https://tts.fjyjsw.com/api/tts/synthesize';

// 初始化curl
$ch = curl_init();

// 设置curl选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'X-App-Id: ' . $app_id,
    'X-Timestamp: ' . $timestamp,
    'X-Sign: ' . $sign
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 执行请求
$response = curl_exec($ch);

// 检查是否有错误
if (curl_errno($ch)) {
    echo '错误: ' . curl_error($ch);
} else {
    // 处理响应
    $result = json_decode($response, true);
    print_r($result);
}

// 关闭curl
curl_close($ch);
                    

响应示例

{
    "code": 200,
    "msg": "Success",
    "data": {
        "task_id": "tts_66f6a0c0704a5",
        "audio_url": "https://tts.fjyjsw.com/audio/098f6bcd4621d373cade4e832627b4f6.mp3",
        "hash": "098f6bcd4621d373cade4e832627b4f6",
        "from_cache": false
    }
}
                    

GET/api/tts/get-audio-stream

获取音频流。

请求参数

参数名 类型 必填 描述
hash string 音频文件的哈希值。

请求示例

cURL示例
curl -X GET "https://tts.fjyjsw.com/api/tts/get-audio-stream?hash=098f6bcd4621d373cade4e832627b4f6" \
  -H "X-App-Id: demo_app" \
  -H "X-Timestamp: 1620000000" \
  -H "X-Sign: 5f4dcc3b5aa765d61d8327deb882cf99" \
  -o output.mp3
                    
JavaScript示例
const appId = 'demo_app';
const appSecret = 'demo_secret_123';
const timestamp = Math.floor(Date.now() / 1000);
const hash = '098f6bcd4621d373cade4e832627b4f6';
const data = ''; // GET请求没有请求体,所以data为空字符串
const sign = md5(appId + timestamp + data + appSecret);

fetch(`https://tts.fjyjsw.com/api/tts/get-audio-stream?hash=${hash}`, {
    method: 'GET',
    headers: {
        'X-App-Id': appId,
        'X-Timestamp': timestamp,
        'X-Sign': sign
    }
})
.then(response => response.blob())
.then(blob => {
    const url = URL.createObjectURL(blob);
    const audio = new Audio(url);
    audio.play();
})
.catch(error => {
    console.error('错误:', error);
});
                    
PHP示例
$app_id = 'demo_app';
$app_secret = 'demo_secret_123';
$timestamp = time();
$hash = '098f6bcd4621d373cade4e832627b4f6';
$data = ''; // GET请求没有请求体,所以data为空字符串

// 计算签名
$sign = md5($app_id . $timestamp . $data . $app_secret);

// API URL
$url = "https://tts.fjyjsw.com/api/tts/get-audio-stream?hash={$hash}";

// 初始化curl
$ch = curl_init();

// 设置curl选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'X-App-Id: ' . $app_id,
    'X-Timestamp: ' . $timestamp,
    'X-Sign: ' . $sign
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 执行请求
$response = curl_exec($ch);

// 检查是否有错误
if (curl_errno($ch)) {
    echo '错误: ' . curl_error($ch);
} else {
    // 获取响应头
    $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    
    // 如果是音频流,输出到浏览器或保存到文件
    if ($content_type == 'audio/mpeg') {
        // 输出到浏览器
        header('Content-Type: audio/mpeg');
        header('Content-Disposition: attachment; filename="audio.mp3"');
        echo $response;
        
        // 或者保存到文件
        // file_put_contents('audio.mp3', $response);
        // echo '音频文件已保存';
    } else {
        // 如果是JSON响应(可能是错误信息),解析并显示
        $result = json_decode($response, true);
        print_r($result);
    }
}

// 关闭curl
curl_close($ch);
                    

响应示例

返回MP3格式的音频流,Content-Type为audio/mpeg。

GET/api/tts/get-audio-info

获取音频文件信息。

请求参数

参数名 类型 必填 描述
hash string 音频文件的哈希值。

请求示例

cURL示例
curl -X GET "https://tts.fjyjsw.com/api/tts/get-audio-info?hash=098f6bcd4621d373cade4e832627b4f6" \
  -H "X-App-Id: demo_app" \
  -H "X-Timestamp: 1620000000" \
  -H "X-Sign: 5f4dcc3b5aa765d61d8327deb882cf99"
                    
JavaScript示例
const appId = 'demo_app';
const appSecret = 'demo_secret_123';
const timestamp = Math.floor(Date.now() / 1000);
const hash = '098f6bcd4621d373cade4e832627b4f6';
const data = ''; // GET请求没有请求体,所以data为空字符串
const sign = md5(appId + timestamp + data + appSecret);

fetch(`https://tts.fjyjsw.com/api/tts/get-audio-info?hash=${hash}`, {
    method: 'GET',
    headers: {
        'X-App-Id': appId,
        'X-Timestamp': timestamp,
        'X-Sign': sign
    }
})
.then(response => response.json())
.then(result => {
    console.log('音频信息:', result);
})
.catch(error => {
    console.error('错误:', error);
});
                    
PHP示例
$app_id = 'demo_app';
$app_secret = 'demo_secret_123';
$timestamp = time();
$hash = '098f6bcd4621d373cade4e832627b4f6';
$data = ''; // GET请求没有请求体,所以data为空字符串

// 计算签名
$sign = md5($app_id . $timestamp . $data . $app_secret);

// API URL
$url = "https://tts.fjyjsw.com/api/tts/get-audio-info?hash={$hash}";

// 初始化curl
$ch = curl_init();

// 设置curl选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'X-App-Id: ' . $app_id,
    'X-Timestamp: ' . $timestamp,
    'X-Sign: ' . $sign
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 执行请求
$response = curl_exec($ch);

// 检查是否有错误
if (curl_errno($ch)) {
    echo '错误: ' . curl_error($ch);
} else {
    // 处理响应
    $result = json_decode($response, true);
    print_r($result);
}

// 关闭curl
curl_close($ch);
                    

响应示例

{
    "code": 200,
    "msg": "Success",
    "data": {
        "hash": "098f6bcd4621d373cade4e832627b4f6",
        "audio_url": "https://tts.fjyjsw.com/audio/098f6bcd4621d373cade4e832627b4f6.mp3",
        "size": 12345,
        "modified_at": 1620000000
    }
}
                    

GET/api/tts/task-status

检查任务状态。

请求参数

参数名 类型 必填 描述
task_id string 任务ID。

请求示例

cURL示例
curl -X GET "https://tts.fjyjsw.com/api/tts/task-status?task_id=tts_66f6a0c0704a5" \
  -H "X-App-Id: demo_app" \
  -H "X-Timestamp: 1620000000" \
  -H "X-Sign: 5f4dcc3b5aa765d61d8327deb882cf99"
                    
JavaScript示例
const appId = 'demo_app';
const appSecret = 'demo_secret_123';
const timestamp = Math.floor(Date.now() / 1000);
const taskId = 'tts_66f6a0c0704a5';
const data = ''; // GET请求没有请求体,所以data为空字符串
const sign = md5(appId + timestamp + data + appSecret);

fetch(`https://tts.fjyjsw.com/api/tts/task-status?task_id=${taskId}`, {
    method: 'GET',
    headers: {
        'X-App-Id': appId,
        'X-Timestamp': timestamp,
        'X-Sign': sign
    }
})
.then(response => response.json())
.then(result => {
    console.log('任务状态:', result);
    if (result.data.status === 'completed') {
        console.log('任务已完成,可以获取音频文件');
    }
})
.catch(error => {
    console.error('错误:', error);
});
                    
PHP示例
$app_id = 'demo_app';
$app_secret = 'demo_secret_123';
$timestamp = time();
$task_id = 'tts_66f6a0c0704a5';
$data = ''; // GET请求没有请求体,所以data为空字符串

// 计算签名
$sign = md5($app_id . $timestamp . $data . $app_secret);

// API URL
$url = "https://tts.fjyjsw.com/api/tts/task-status?task_id={$task_id}";

// 初始化curl
$ch = curl_init();

// 设置curl选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'X-App-Id: ' . $app_id,
    'X-Timestamp: ' . $timestamp,
    'X-Sign: ' . $sign
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 执行请求
$response = curl_exec($ch);

// 检查是否有错误
if (curl_errno($ch)) {
    echo '错误: ' . curl_error($ch);
} else {
    // 处理响应
    $result = json_decode($response, true);
    print_r($result);
    
    if ($result['code'] === 200 && $result['data']['status'] === 'completed') {
        echo '任务已完成,可以获取音频文件';
    }
}

// 关闭curl
curl_close($ch);
                    

响应示例

{
    "code": 200,
    "msg": "Success",
    "data": {
        "task_id": "tts_66f6a0c0704a5",
        "status": "completed",
        "message": "Task completed"
    }
}
                    

错误码

错误码 描述
400 请求参数错误。
401 认证失败。
500 服务器内部错误。