javascript - Hide referrer header in API request -
i need make requests google translate text-to-speech api. have enabled key keep getting blocked no access-control-allow-origin.
i've posted more here:
google translate api - no access control origin text speech
the following sources, http://weston.ruter.net/2009/12/12/google-tts/ , request google text-to-speech api
google returns 404 error if http request contains referer header other empty string.
given request like:
$.get('https://translate.google.com/translate_tts?key='+mykey+'&ie=utf-8&tl=en&q=hello+world', function (returned_data) {
how hide or remove referrer header i'm not blocked?
this source says put https://href.li/...
in front. changed to:
$.get('https://href.li/?https://translate.google.com/translate_tts?key='+key+'&ie=utf-8&tl=zh-cn&q=你好',
and still blocked.
server-side attempt: no response.
this blog provides server-side script sets referrer empty string. says:
this gets information access point , spits out right away. before requests data using file_get_contents, referer header set empty string.
/php/testphp.php:
$qs = http_build_query(array("ie" => "utf-8","tl" => $_get["tl"], "q" => $_get["q"])); $ctx = stream_context_create(array("http"=>array("method"=>"get","header"=>"referer: \r\n"))); $soundfile = file_get_contents("http://translate.google.com/translate_tts?".$qs, false, $ctx); header("content-type: audio/mpeg"); header("content-transfer-encoding: binary"); header('pragma: no-cache'); header('expires: 0'); echo($soundfile);
index.php (root):
<audio controls="controls" autoplay="autoplay" style="display:none;"> <source src="/php/testphp.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog." type="audio/mpeg" /> </audio>
tail -f apache error logs:
php notice: undefined index: tl in /users/myname/sites/app/melonjs-dev/testphp.php on line 4, referer: http://melon.localhost/
php warning: file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog): failed open stream: http request failed! http/1.0 404 not found\r\n in /users/myname/sites/app/melonjs-dev/testphp.php on line 6, referer: http://melon.localhost
tail -f access log: shows tl
param getting passed in 200:
get /testphp.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog http/1.1" 200 -
jsonp attempt: thought perhaps wrapping return object in <script>
solve issue. no response.
$.ajax({ url: 'https://translate.google.com/translate_tts?key='+key+'&ie=utf-8&tl=zh-cn&q=你好', type: 'get', datatype: 'jsonp', error: function(xhr, status, error) { alert("error"); }, success: function(json) { alert("success"); } });
html/js text-to-speech attempt: 404 block
js:
<script> $(function() { $('a.say').on('click', function(e) { e.preventdefault(); var text = $('input[name="text"]').val(); text = encodeuricomponent(text); console.log(text); //var url = 'https://translate.google.com/translate_tts?&ie=utf-8&tl=zh-cn&q=' + text; var url = 'https://translate.google.com/translate_tts?ie=utf-8&q=' + text + '&tl=en'; $('audio').attr('src', url).get(0).play(); }); }); </script>
html:
<input type="text" name="text"> <a href="#" class="say">say it</a> <audio src="" class="speech" hidden></audio>
following tutorial: https://www.youtube.com/watch?v=dotknxmg9qy
why php script failing:
if take @ error response php:
php warning: file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog)
you'll notice url file_get_contents
requesting not contain tl
parameter. causing 404 returned. can visit page directly in browser:
https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog
the above returns 404 response :(
https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog&tl=en
but after add nice new shiny tl=en
parameter, works :).
Comments
Post a Comment