php - Google Translate API Text to speech - setting encoding for non-roman characters -
i'm using google translate's unofficial text-to-speech api (i've posted more info on here).
the api endpoint looks like: https://translate.google.com/translate_tts?ie=utf-8&tl=en&q=hello%20world
making traditional api requests words, no-access-control-origin , 404 blocks. around this, i've followed the php script in blog strips out referrer before making request (more info on attempts here).
i'm able english work, need work chinese. unfortunately, when pass in 你好, voice seems narrate gibberish. however, if add directly browser, narrates perfectly.
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 , return stream context $soundfile = file_get_contents("https://translate.google.com/translate_tts?".$params, false, $ctx); //reads file string (string 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 shows:
get /testphp.php?translate_tts?ie=utf-8&tl=zh-cn&q=%e4%bd%a0%e5%a5%bd http/1.1" 200 13536
this seems okay. can see, q
query param value, 你好, has been converted. fine because still works if put in browser:
https://translate.google.com/translate_tts?ie=utf-8&tl=zh-cn&q=%e4%bd%a0%e5%a5%bd
tail -f apache error_logs shows:
php notice: undefined index: ie in /users/danturcotte/sites/personal_practice/melonjs-dev/testphp.php on line 4, referer: http://melon.localhost/
i'm not sure how happening, or if it's contributing screwing pronunciation. i'm thinking words may reading off parts of ie
index?
the query params browser side seem registering,
and can see apache access_logs, ie=utf-8
param being set fine.
so questions are:
i've added
header('content-type: text/plain; charset=utf-8');
testphp.php
file ensure encoding going through fine. contributing problem?i'm building uri query string such:
$params = http_build_query(array("ie" => $_get['ie'],"tl" => $_get["tl"], "q" => $_get["q"]));
, how can there undefined indexie
?
the problem in url:
get /testphp.php?translate_tts?ie=utf-8&tl=zh-cn&q=%e4%bd%a0%e5%a5%bd
you have 2 question marks, means php get:
array ( [translate_tts?ie] => utf-8 [tl] => zh-cn [q] => 你好 )
instead need like:
get /testphp.php?translate_tts=value&ie=utf-8&tl=zh-cn&q=%e4%bd%a0%e5%a5%bd
Comments
Post a Comment