繁体   English   中英

使用Google Translate API时出现错误400

[英]Error 400 when using Google Translate API

我目前在使用Google Translate API时遇到错误400“您的客户发出了格式错误或非法的请求”。

当我直接在浏览器中使用该url时,可以并返回翻译。 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

这是我的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);

有任何想法吗 ? 谢谢。

确定,找到解决方案。

我必须补充:

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

暂无
暂无

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

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