【发布时间】: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