Files
fast_response/digital_doctor/application/api/controller/Service.php
2025-03-17 10:56:09 +08:00

88 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* description
* authorwh
* email
* createTime{2024/10/21} {14:52}
*/
namespace app\api\controller;
use wanghua\general_utility_tools_php\gpt\chat\ChatGPT;
use wanghua\general_utility_tools_php\Mmodel;
use wanghua\general_utility_tools_php\tool\Tools;
class Service extends BaseHttpApi
{
/**
* desc文本转语音
*
* 语音转换服务
*
* api/Service/azurettsAudio
*
* 参数:
* text: 文本内容
*
* authorwh
*/
public function azurettsAudio()
{
$text = input('text','Bagaimana keadaan pernafasan anda?');
$data_raw = input('data_raw','ms-MY');
$data_neural = input('data_neural','YasminNeural');
$service_config = config('voice_to_text_service');
//回答
$url = $service_config['base_url'];//"https://eastasia.tts.speech.microsoft.com/cognitiveservices/v1";
$subscriptionKey = $service_config['APIKey'];//"090287baa7b14fcfb060a70bd1863f2f";
$outputFormat = "audio-48khz-192kbitrate-mono-mp3";
$xmlContent = <<<XML
<?xml version='1.0'?>
<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='{$data_raw}'>
<voice name='{$data_raw}-{$data_neural}'>
{$text}
</voice>
</speak>
XML;
$options = array(
'http' => array(
'header' => "Ocp-Apim-Subscription-Key: $subscriptionKey\r\n" .
"Content-Type: application/ssml+xml\r\n" .
"X-Microsoft-OutputFormat: $outputFormat\r\n" .
"User-Agent: MyTTSApp\r\n", // 添加 User-Agent 头
'method' => 'POST',
'content' => $xmlContent,
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
// 处理错误
//echo "请求失败";
return json(Tools::set_fail('请求失败'));
} else {
$date = date("YmdHis");
$root_path = Tools::get_root_path();
$request_path = '/audio/azure_tts';
$save_path = 'public'.$request_path;
$filepath = $root_path . $save_path;
if (!file_exists($filepath)) {
mkdir($filepath, 0777, true); // 确保目录存在
}
$filename = "/output_$date.mp3";
file_put_contents($filepath . $filename, $result);
//echo "音频文件已生成: $filename";
$domain = request()->domain();
return json(Tools::set_ok('请求成功',$domain.$request_path.$filename));
}
}
function test(){
}
}