简体   繁体   English

使用Google Translate API时出现错误400

[英]Error 400 when using Google Translate API

I am currently faced with an error 400 "Your client has issued a malformed or illegal request" when using Google Translate API. 我目前在使用Google Translate API时遇到错误400“您的客户发出了格式错误或非法的请求”。

When I directly use the url into the browser, it's OK and return the translation. 当我直接在浏览器中使用该url时,可以并返回翻译。 cf. cf. https://www.googleapis.com/language/translate/v2?key=[KEY]&target=fr&q=Centrally%20located,%20huge%20room,%20super%20design%20of%20the%20bathroom,%20bathtube&source=en https://www.googleapis.com/language/translate/v2?key=[KEY]&target=fr&q=Centrally%20located,%20huge%20room,%20super%20design%20of%20the%20bathroom,%20bathtube&source=en

Here is my PHP code : 这是我的PHP代码:

$base_url = 'https://www.googleapis.com/language/translate/v2?';
$url = $base_url.'key='.$this->api_key.'&target='.$target.'&q='.$text;
if ($source) {
   $url .= '&source='.$source;
}
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HEADER, 0);
$contents = curl_exec($c);

Any ideas ? 有任何想法吗 ? Thanks. 谢谢。

OK, found the solution. 确定,找到解决方案。

I must add : 我必须补充:

$text = strip_tags($text);
$text = str_replace(' ', '+', $text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM