在 API 请求中隐藏引用标头 [英] Hide referrer header in API request
问题描述
我需要向 Google Translate Text-To-Speech API 提出请求.我有一个已启用的密钥,但一直被 No Access-Control-Allow-Origin 阻止.
I need to make requests to Google Translate Text-To-Speech API. I have an enabled key but keep getting blocked by No Access-Control-Allow-Origin.
我在这里发布了更多相关信息:
I've posted more about it here:
以下来源,http://weston.ruter.net/2009/12/12/google-tts/ 和 向谷歌请求Text-To-Speech API 说
The following sources, http://weston.ruter.net/2009/12/12/google-tts/ and Request to Google Text-To-Speech API say that
如果 HTTP 请求包含 Referer,Google 会返回 404 错误不是空字符串的标头.
Google returns a 404 error if the HTTP request contains a Referer header other than an empty string.
给出如下请求:
$.get('https://translate.google.com/translate_tts?key='+myKey+'&ie=utf-8&tl=en&q=Hello+world',
function (returned_data) {
如何隐藏或删除引荐来源网址以免被阻止?
How do I hide or remove the referrer header so I'm not blocked?
这个来源说把https://href.li/...
在前面.所以我把它改成:
this source says to put https://href.li/...
in front. So I changed it to:
$.get('https://href.li/?https://translate.google.com/translate_tts?key='+key+'&ie=utf-8&tl=zh-CN&q=你好',
我还是被屏蔽了.
服务器端尝试:无响应.
本博客提供将引用者设置为空字符串的服务器端脚本.他说:
This blog provides a server-side script which sets the referrer to an empty string. He says:
这会从该接入点获取信息并立即将其吐出.但在使用 file_get_contents 请求数据之前,Referer 标头设置为空字符串.
This gets the information from that access point and spits it out right away. But before it requests the data using file_get_contents, the Referer header is set to an 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(根):
<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 错误日志:
PHP 注意:未定义索引:tl in/Users/myname/Sites/app/melonJS-dev/testPHP.php 第 4 行,referer:http://melon.localhost/
PHP Notice: Undefined index: tl in /Users/myname/Sites/app/melonJS-dev/testPHP.php on line 4, referer: http://melon.localhost/
PHP 警告:file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog):无法打开流:HTTP 请求失败!HTTP/1.0 404 Not Found\r\n in/Users/myname/Sites/app/melonJS-dev/testPHP.php on line 6,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 to 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 访问日志: 显示以 200 传入的 tl
参数:
tail -f access log: Shows the tl
param getting passed in with a 200:
获取/testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dogHTTP/1.1" 200 -
GET /testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog HTTP/1.1" 200 -
<小时>
JSONP 尝试: 所以我想也许将返回对象包装在 中可以解决这个问题.我没有得到任何回应.
JSONP Attempt: so I thought perhaps wrapping the return object in a <script>
would solve the issue. I get 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 文本转语音尝试: 404 块
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>
遵循本教程:https://www.youtube.com/watch?v=DOtkNxmg9QY
推荐答案
PHP 脚本失败的原因:
如果您查看 PHP 的错误响应:
If you take a look at your error response from PHP:
PHP Warning: file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog)
您会注意到 file_get_contents
请求的 url 不包含 tl
参数.这会导致返回 404.您可以直接在浏览器中访问该页面:
You'll notice that the url file_get_contents
is requesting does not contain a tl
parameter. This is causing a 404 to be returned. You can visit the page directly in your browser:
https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog
以上返回404响应:(
The above returns a 404 response :(
https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog&tl=en
但是在我们添加一个漂亮的新 tl=en
参数后,它起作用了:)
But after we add a nice new shiny tl=en
parameter, it works :).
这篇关于在 API 请求中隐藏引用标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!