5

I am trying to use free Google Translate API which is extracted from Firefox's S3 Google Translator addon, ie.

https://translate.google.com/translate_a/single?client=t&sl=auto&
tl=en&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t
&dt=at&ie=UTF-8&oe=UTF-8&otf=2&srcrom=1&ssel=0&tsel=0&q=Hello

in PHP cURL ie.

$isPOST=isset($_POST) && !empty($_POST);
$q=$isPOST ? $_POST['q'] : $_GET['q'];

$url='https://translate.google.com/translate_a/single';
$data='client=t&sl=auto&tl=en&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&srcrom=1&ssel=0&tsel=0&q='.$q;
$ch=curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_URL, !$isPOST ? $url.'?'.$data : $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if($isPOST){
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$return=curl_exec($ch);
curl_close($ch);

I am calling this page using ajax..

$.ajax({
    type: text.length>750 ? 'post' : 'get',
    url: 'translate.php',
    data: 'q='+text,
    success: function(d){ alert(d); }
});

but doing this all, I get this response from Google Translate, ie.

Error: 400. That’s an error.
Your client has issued a malformed or illegal request. That’s all we know.

Please, help me solve this error and get the translated text..

Vaibhav Gupta
  • 1,592
  • 1
  • 13
  • 23

3 Answers3

1

I checked your URL in your browser it shows 400 Error. it means illegal request. try http://www.sitepoint.com/using-google-translate-api-php/ this URL.

<?php
    $apiKey = '<paste your API key here>';
    $text = 'Hello world!';
    $url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($text) . '&source=en&target=fr';

    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($handle);                 
    $responseDecoded = json_decode($response, true);
    curl_close($handle);

    echo 'Source: ' . $text . '<br>';
    echo 'Translation: ' . $responseDecoded['data']['translations'][0]['translatedText'];
?>
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
  • but if you see S3 Google Translator Firefox plugin source, its using same url to translate the text... – Vaibhav Gupta Jul 03 '15 at 11:23
  • also, if you try to open the url ie. https://translate.google.com/translate_a/single?client=t&sl=auto&tl=en&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&srcrom=1&ssel=0&tsel=0&q=Hello, then it'll give the translation in a txt file, so its working... – Vaibhav Gupta Jul 03 '15 at 11:25
  • The problem is simply to url_encode the data.. i had the same issue, and I fixed it by simply url_encoding the data and leaving the method as GET. that's why it's working from the browser because anything you paste in the addressbar is url encoded before being posted – Dany Balian May 17 '20 at 09:00
1

Sorry, I tried to POST with same code and it worked.. Thank all.

Vaibhav Gupta
  • 1,592
  • 1
  • 13
  • 23
1

I have the same issue in a Visual Basic 6 Project and Thamaraiselvam's comment guided me to the correct direction.. I was building the URL correctly and if I try it in the browser it works, but in the http component of vb6 it wasn't working. the solution was to simply url_encode the data sent. (putting it in the browser was automatically doing it)

hope this helps someone else.

Dany Balian
  • 608
  • 7
  • 16