Google 翻译 API 文本到语音 - 为非罗马字符设置编码

Google Translate API Text to speech - setting encoding for non-roman characters

我正在使用 Google Translate 的非官方文本转语音 API(我已经发布了更多信息 here)。

API 端点类似于:https://translate.google.com/translate_tts?ie=utf-8&tl=en&q=Hello%20World

进行传统的 API 字词请求,我得到了 No-access-control-origin 和 404 块。为了解决这个问题,我遵循了 the php script in this blog which strips out the referrer before making the request (more info on my attempts ).

我可以用英语工作,但我需要这个才能用中文。不幸的是,当我输入像你好这样的东西时,声音似乎在说胡言乱语。但是,如果您将其直接添加到您的浏览器中,它的叙述就完美了。

https://translate.google.com/translate_tts?ie=utf-8&tl=zh-CN&q=你好

HTML:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<audio controls="controls" autoplay="autoplay" style="display:none;">
    <source src="testPHP.php?translate_tts?ie=utf-8&tl=zh-CN&q=你好" type="audio/mpeg" />
</audio>

testPHP.php:

<?php
//https://translate.google.com/translate_tts?ie=UTF-8&q=' + text + '&tl=en
header('Content-type: text/plain; charset=utf-8');
$params = http_build_query(array("ie" => $_GET['ie'],"tl" => $_GET["tl"], "q" => $_GET["q"]));
$ctx = stream_context_create(array("http"=>array("method"=>"GET","header"=>"Referer: \r\n"))); //create and return stream context
$soundfile = file_get_contents("https://translate.google.com/translate_tts?".$params, false, $ctx); //reads file into string (string with params[utf-8, tl, q], use include path bool, custom context resource headers)

header("Content-type: audio/mpeg");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');

echo($soundfile);

tail -f apache access_logs 显示:

GET /testPHP.php?translate_tts?ie=utf-8&tl=zh-CN&q=%E4%BD%A0%E5%A5%BD HTTP/1.1" 200 13536

这似乎还可以。如您所见,q 查询参数值“你好”已被转换。这很好,因为如果你把它放在浏览器中它仍然有效:

https://translate.google.com/translate_tts?ie=utf-8&tl=zh-CN&q=%E4%BD%A0%E5%A5%BD

tail -f apache error_logs 显示:

PHP Notice: Undefined index: ie in /Users/danturcotte/Sites/personal_practice/melonJS-dev/testPHP.php on line 4, referer: http://melon.localhost/

我不确定这是怎么回事,或者它是否有助于搞砸发音。我在想这些词可能会读出 ie 索引的部分内容?

浏览器端的查询参数似乎正在注册,

正如您从 apache access_logs 中看到的那样,ie=utf-8 参数设置正常。

所以我的问题是:

问题出在你的URL:

GET /testPHP.php?translate_tts?ie=utf-8&tl=zh-CN&q=%E4%BD%A0%E5%A5%BD

你有两个问号,也就是说PHP会得到:

Array 
( 
[translate_tts?ie] => utf-8 
[tl] => zh-CN 
[q] => 你好 
)

相反,您需要执行以下操作:

GET /testPHP.php?translate_tts=value&ie=utf-8&tl=zh-CN&q=%E4%BD%A0%E5%A5%BD