phpgoogle-cloud-platformgoogle-translategoogle-translation-apigoogle-cloud-translate

Google cloud translate API v2 - getting errors with large text pieces


I have this code which I am trying to use to translate my website page content:

  $url = "https://translation.googleapis.com/language/translate/v2";
  $sendParamsObj = [
      "key" => "insert api key" 
      ,
      "source" => 'en-us'
      ,
      "target" => 'da-dk' 
      ,
      "q" => 'smaller amount of <strong>content</strong> to translate'
  ];                    
  $myBodyReturn = null;          
  if (true) {
    /*
      This errors: Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
    */  
    $sendParamsStrJson = json_encode($sendParamsObj);
    $sendOptions = array(
      'http' => array(
        'method'  => 'POST',
        'content' => $sendParamsStrJson,                
        'header'=>  'Content-Type: application/json'
      )
    );                          
    $myContext = stream_context_create($sendOptions);            
    $myBodyReturn = file_get_contents($url, false, $myContext); 
  }  
  else {            
    /*
     For large text/html pieces this probably exceeds GET length (?) and erros: 
       Failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request 
    */ 
    $sendParamsQuery = http_build_query($sendParamsObj);
    $myBodyReturn = file_get_contents($url . "?" . $sendParamsQuery);                        
  }
 var_dump($myBodyReturn);          

As can be seen, if I use the top "if (true)" solution using POST and JSON I get error 403...

But if I use "else" solution building GET query this fails with error 400 for large text/HTML pieces

...

Trying something different also gives 403:

$url = "https://translation.googleapis.com/language/translate/v2";
$sendParamsArr = array(
    "key" => "my key" 
    ,
    "source" => 'en-us'
    ,
    "target" => 'da-dk' 
    ,
    "q" => 'smaller amount of <strong>content</strong> to translate'
);                                                  
$data_json = json_encode($sendParamsArr);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_json)
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$myBodyReturn = curl_exec($ch);
$res_responsecode_page = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);                  
curl_close($ch);

...

So it appears to work when using GET (but that only supports short text pieces) but not when using POST/JSON

Since it works using GET it is probably not API key issue. Any ideas?


Solution

  • POST should be supported, but I don't see https://cloud.google.com/translate/docs/reference/rest/v2/translate mentioning JSON, it says "query parameters".

    Try making an application/x-www-form-urlencoded request.

    return HTML tags changed, e.g. "<" becomes "u003c"

    Actually \u003C, I suppose? That is what you would get with PHP's json_encode with the JSON_HEX_TAG flag set. Not sure why they would use different encoding options for GET vs POST, but after you decoded this JSON, you should get a < either way.