2016-10-05 80 views
0

當您使用Google翻譯API時,我目前遇到錯誤400「您的客戶發出格式錯誤或非法請求」。使用Google翻譯API時出現錯誤400

當我直接使用網址到瀏覽器,它是確定的,並返回翻譯。 比較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); 

任何想法?謝謝。

回答

0

好的,找到了解決辦法。

我必須補充:

$text = strip_tags($text); 
$text = str_replace(' ', '+', $text); 
相關問題