php中的谷歌云文本语音转换

php中的谷歌云文本语音转换,php,google-cloud-platform,text-to-speech,google-text-to-speech,Php,Google Cloud Platform,Text To Speech,Google Text To Speech,我正试图在我的php网站上使用google的文本到语音转换,并将其托管在一个实时Cpanel服务器上 我已经启用了文本到语音API,在凭证部分创建了API密钥,还从创建服务帐户密钥页面下载了凭证的json文件 然后,我从Github下载了示例文件,并使用composer构建了库 现在我不知道把钥匙放在哪里。在任何地方,都需要导出Shell中的密钥,但这将适用于1个打开的命令提示符会话,并且每次都必须导出 因为我想在基于cpanel的实时主机上运行这段代码,所以我认为不可能导出 密码里有我可以传递

我正试图在我的php网站上使用google的文本到语音转换,并将其托管在一个实时Cpanel服务器上

我已经启用了文本到语音API,在凭证部分创建了API密钥,还从创建服务帐户密钥页面下载了凭证的json文件

然后,我从Github下载了示例文件,并使用composer构建了库

现在我不知道把钥匙放在哪里。在任何地方,都需要导出Shell中的密钥,但这将适用于1个打开的命令提示符会话,并且每次都必须导出

因为我想在基于cpanel的实时主机上运行这段代码,所以我认为不可能导出

密码里有我可以传递钥匙的地方吗

关于stackoverflow的文章:第一个答案将CURL的响应导出到synthesis text.txt,但我们需要mp3输出

另一个答案是我们应该使用jq,但由于它是一个共享的hsoting服务器,我不确定我们是否可以安排jq

有办法解决这个问题吗


更新

在@V.Tur参考答案后尝试了以下代码

$params = [
    "audioConfig"=>[
        "audioEncoding"=>"MP3",
        "pitch"=> "1",
        "speakingRate"=> "1",
        "effectsProfileId"=> [
            "medium-bluetooth-speaker-class-device"
          ]
    ],
    "input"=>[
        "ssml"=>'<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
                  standard <break time=\"1s\"/>is defined by the
                  <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>'
    ],
    "voice"=>[
        "languageCode"=> "hi-IN",
        "name" =>"hi-IN-Wavenet-B",
        'ssmlGender'=>'MALE'
    ]
];
$data_string = json_encode($params);
$speech_api_key = "My_Key_Here";
$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
$handle = curl_init($url);

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string);  
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, [                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string)
    ]                                                                       
);
$response = curl_exec($handle);              
$responseDecoded = json_decode($response, true);  
curl_close($handle);
if($responseDecoded['audioContent']){
    return $responseDecoded['audioContent'];                
} 
$params=[
“audioConfig”=>[
“音频编码”=>“MP3”,
“音高”=>“1”,
“speakingRate”=>“1”,
“effectsProfileId”=>[
“中等蓝牙扬声器类设备”
]
],
“输入”=>[
“ssml”=>“ssml”
标准由
W3C。”
],
“声音”=>[
“languageCode”=>“hi-IN”,
“名称”=>“hi-IN-Wavenet-B”,
'ssmlGender'=>'MALE'
]
];
$data\u string=json\u encode($params);
$speech\u api\u key=“My\u key\u Here”;
$url='1https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $语音键;
$handle=curl\u init($url);
curl_setopt($handle,CURLOPT_CUSTOMREQUEST,“POST”);
curl_setopt($handle,CURLOPT_POSTFIELDS,$data_string);
curl_setopt($handle,CURLOPT_RETURNTRANSFER,true);
curl_setopt($handle,CURLOPT_HTTPHEADER,[
'内容类型:application/json',
“内容长度:”.strlen($data_字符串)
]                                                                       
);
$response=curl\u exec($handle);
$responseDecoded=json_decode($response,true);
卷曲关闭($handle);
if($responseDecoded['audioContent'])){
返回$responseDecoded['audioContent'];
} 
我下载了音频,但我在ssml中提到的暂停/中断不起作用。我尝试将数据传递给$params,如下所示

$params = "{
    'input':{
     'ssml':'<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
          standard <break time=\"1s\"/>is defined by the
          <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>'
    },
    'voice':{
      'languageCode':'en-us',
      'name':'en-US-Standard-B',
      'ssmlGender':'MALE'
    },
    'audioConfig':{
      'audioEncoding':'MP3'
    }
}";
$params=”{
“输入”:{
“ssml”:“ssml
标准由
W3C。”
},
“声音”:{
“语言代码”:“en-us”,
“名称”:“en-US-Standard-B”,
“ssmlGender”:“男性”
},
“音频配置”:{
“音频编码”:“MP3”
}
}";
但我得到了以下错误:

数组([错误]=>数组([代码]=>400[消息]=>无效JSON 收到有效负载。未知名称“”:根元素必须是消息。 [状态]=>无效的\u参数[详细信息]=>数组([0]=>数组( [@type]=>type.googleapis.com/google.rpc.BadRequest[fieldViolations] =>数组([0]=>数组([description]=>接收到无效的JSON负载。未知名称“”:根元素必须是消息。))


如何解决此问题?

在我的工作示例文本到语音下,您可以根据需要重做:

public static function getSound($text)
        {            
            
            $text = trim($text);

            if($text == '') return false;
            
            $params = [
                "audioConfig"=>[
                    "audioEncoding"=>"LINEAR16",
                    "pitch"=> "1",
                    "speakingRate"=> "1",
                    "effectsProfileId"=> [
                        "medium-bluetooth-speaker-class-device"
                      ]
                ],
                "input"=>[
                    "text"=>$text
                ],
                "voice"=>[
                    "languageCode"=> "en-US",
                    "name" =>"en-US-Wavenet-F"
                ]
            ];

            $data_string = json_encode($params);

            $url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
            $handle = curl_init($url);
            
            curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); 
            curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string);  
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($handle, CURLOPT_HTTPHEADER, [                                                                          
                'Content-Type: application/json',                                                                                
                'Content-Length: ' . strlen($data_string)
                ]                                                                       
            );
            $response = curl_exec($handle);              
            $responseDecoded = json_decode($response, true);  
            curl_close($handle);
            if($responseDecoded['audioContent']){
                return $responseDecoded['audioContent'];                
            } 

            return false;  
        }

using:
public static function saveSound($text)
   {
      $speech_data = SpeechAPI::getSound($text);//see method upper

      if($speech_data) {                
         $file_name = strtolower(md5(uniqid($text)) . '.mp3');
         $path = FileUpload::getFolder();//just return directory path
         if(file_put_contents($path.$file_name, base64_decode($speech_data))){
             return $file_name;
             }
         }

        return null;
   }
对于SSML标准,需要更改输入参数:

$text = "<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
            standard <break time=\"1s\"/>is defined by the
            <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>";
$params = [
    "audioConfig"=>[
    "audioEncoding"=>"LINEAR16",
    "pitch"=> "1",
    "speakingRate"=> "1",
    "effectsProfileId"=> [
        "medium-bluetooth-speaker-class-device"
       ]
     ],
     "input"=>[
         //"text"=>$text
         "ssml" => $text
          ],
          "voice"=>[
              "languageCode"=> "en-US",
              "name" =>"en-US-Wavenet-F"
            ]
         ];
$text=“SSML
标准由
W3C。”;
$params=[
“audioConfig”=>[
“音频编码”=>“LINEAR16”,
“音高”=>“1”,
“speakingRate”=>“1”,
“effectsProfileId”=>[
“中等蓝牙扬声器类设备”
]
],
“输入”=>[
//“text”=>$text
“ssml”=>$text
],
“声音”=>[
“语言代码”=>“美国英语”,
“名称”=>“en-US-Wavenet-F”
]
];

关于choose audioEncoding-

您自己对此问题做过研究吗?请看谷歌和它的通用G.Cloud@deltazero,我在问题描述中指定了很多东西。我的脑海中浮现的这些URL只是你共享的,但我无法完全做到,这就是为什么我在这里发布查询以获得回复的原因。但我还需要获得wavenet提供的语言和声音列表。还请分享如何将您得到的回答转换为mp3I编辑的答案。我在那个里得到的可用语言和其他tunning——将文本转换为语音的部分付诸实施:嘿,看起来事情对我来说很好,但它不停地说话,不停地休息。如何在两个句子之间添加延迟我将mp3返回到js脚本中,并在队列中以延迟方式播放它们。@T.Vur那么你的意思是,对于每个句子,你都会得到一个单独的mp3文件?但这对更长的文本不起作用。你对SSML有什么想法吗?如何在SSML中传递有效负载?我已经根据我们的答案更新了我的帖子,你能看一下吗?