我们可以在没有谷歌密钥的情况下使用谷歌翻译吗?
我在几个网站 (http://goo.gl/usUSP) 中找到了这段代码:
<?php
// Basic request parameters:
// s = source language
// d = destination language
// q = Text to be translated
$s = $_REQUEST['s'];
if(!$s)echo "translate.php?s=en&d=es&q=Hello%20World";
$d = $_REQUEST['d'];
$lang_pair = urlencode($s.'|'.$d);
$q = urlencode($_REQUEST['q']);
// Google's API translator URL
$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".$q."&langpair=".$lang_pair;
// Make sure to set CURLOPT_REFERER because Google doesn't like if you leave the referrer out
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.yoursite.com/translate.php");
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body, true);
echo $json['responseData']['translatedText'];
?>
好的,基本上据我所知,使用谷歌翻译 API 的每个调用都必须提供有效的密钥(用户的密钥)。
然而,从上面的示例代码来看,没有给出密钥并且它可以工作!证明: http://juzcode.com/translate.php ?s=en&d=es&q=Hello%20World。
关于这个问题我有 3 个问题:
1)这怎么可能? (我不太懂 php)
2) Google 施加的限制是每个 Google-key 100k 个字符。该示例不使用密钥。所以基本上我可以无限使用?
3) 是否违反 http://code.google.com/apis/language/翻译/terms.html ? (我真的试着读过它!但我不是律师,我只是想确定一下)
I found this code in a couple of websites (http://goo.gl/usUSP):
<?php
// Basic request parameters:
// s = source language
// d = destination language
// q = Text to be translated
$s = $_REQUEST['s'];
if(!$s)echo "translate.php?s=en&d=es&q=Hello%20World";
$d = $_REQUEST['d'];
$lang_pair = urlencode($s.'|'.$d);
$q = urlencode($_REQUEST['q']);
// Google's API translator URL
$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".$q."&langpair=".$lang_pair;
// Make sure to set CURLOPT_REFERER because Google doesn't like if you leave the referrer out
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.yoursite.com/translate.php");
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body, true);
echo $json['responseData']['translatedText'];
?>
Ok basically from what I know, every call using the google translate API must provide a valid key (the user's key).
However from the example code above no key is given and it works! prove: http://juzcode.com/translate.php?s=en&d=es&q=Hello%20World.
I've got 3 questions regarding this issue:
1) How is this even possible? (i'm not very php literate)
2) The limit imposed by Google is 100k characters per Google-key. The example uses no key. So basically I'd have unlimited usage?
3) Does it violate http://code.google.com/apis/language/translate/terms.html ? (I've tried to read it, really! But I'm no lawyer, I'd just want to be sure)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是用于基于 AJAX 的调用的 REST API,而不是基于自动化服务器的 API。
你不想这样做,因为:
使用 PHP 中的curl 被视为自动请求。请改用 Rest API。您将需要一把钥匙。
The is the REST API meant for AJAX based calls, not the automated server based API.
You don't want to do that because:
using curl from PHP is considered an automated request. Use the Rest API instead. You will need a key.