如何使用Google Cloud Translate API通过php和curl翻译PDF文档

问题描述 投票:0回答:1

我正在尝试使用 Google Translate API 自动翻译 PDF 文档,并且我想使用 php/curl。 翻译文本的代码非常容易处理:

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);
}

但是我无法成功编写发送 PDF 文件并接收翻译的代码。 这显然是可能的,如 Google Translate API 帮助中所述。

另外,如果我正确阅读文档,PDF 会作为字节流发送,但响应是存储在云中的文档?是不是收不到文件内容了?

非常感谢。

php pdf curl translation google-translation-api
1个回答
0
投票

我建议你使用

richardsonoge/pdf-translator
https://github.com/richardsonoge/pdf-translator 包我认为它可以完全按照你想要的方式做,最重要的是它不会给你带来任何问题如果您的项目有大量流量,因为它配备了代理,可以帮助您,这样 Google 就不会阻止您的服务器。

© www.soinside.com 2019 - 2024. All rights reserved.