How to use Google Cloud Translate API to translate PDF documents with php and curl

147 Views Asked by At

I am trying to use the Google Translate API to automatically translate PDF documents and I want to use php/curl. The code for translating text is pretty easy to handle:

function _translate_text(){
  $apiKey = 'myAPIKey';
  $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);
  curl_setopt($handle, CURLOPT_REFERER, "mysite"); //without this the authentication fails
  $response = curl_exec($handle);
  $responseDecoded = json_decode($response, true);
  curl_close($handle);
  echo 'Translation: ' . var_dump($responseDecoded);
}

However I cannot succed to write a code that would send a PDF file and receive the translation. It is clearly possible, as described in the Google Translate API help.

Also, if I read the documentation correctly, the PDF is sent as a stream of bytes, but the response is a document stored in cloud? Is it not possible to receive the file contents back?

Thank you very much.

0

There are 0 best solutions below