【问题标题】:Error 400 when using Google Translate API使用 Google Translate API 时出现错误 400
【发布时间】:2016-10-05 15:27:55
【问题描述】:

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

当我直接使用url进入浏览器时,就OK了,返回翻译。 参看。 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);

有什么想法吗?谢谢。

【问题讨论】:

    标签: curl google-translate


    【解决方案1】:

    好的,找到了解决办法。

    我必须补充:

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

    【讨论】:

      猜你喜欢
      • 2018-12-04
      • 2014-01-31
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 2015-02-27
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多