admin管理员组

文章数量:1405339

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

;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='';
$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..

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

https://translate.google./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./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..

Share Improve this question asked Jul 3, 2015 at 10:26 Vaibhav GuptaVaibhav Gupta 1,6121 gold badge14 silver badges23 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

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

<?php
    $apiKey = '<paste your API key here>';
    $text = 'Hello world!';
    $url = 'https://www.googleapis./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'];
?>

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

I have the same issue in a Visual Basic 6 Project and Thamaraiselvam's ment 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 ponent 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.

本文标签: javascript400 bad request in google translate free api using curlStack Overflow