【问题标题】:Using google translate API with cURL使用带有 cURL 的谷歌翻译 API
【发布时间】:2016-05-02 23:52:51
【问题描述】:

我正在尝试使用 Google Translate API 来翻译用户在我的基于 php 的网站上输入的文本。到目前为止,我有:

<?php

  $google_url = "https://www.googleapis.com/language/translate/v2?key=[API KEY]&q=apple&source=en&target=de";

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

  $google_res = $responseDecoded['data']['translations'][0]['translatedText'];

  print_r($response);
?>

这没有返回任何东西,我不知道出了什么问题。我知道 API 设置正确,因为当我将 url 输入浏览器时,它会返回以下内容:

{
 "data": {
  "translations": [
   {
    "translatedText": "Apfel"
   }
  ]
 }
}

这一定与我从 This Site 获取的代码有关

任何帮助将不胜感激,因为我完全被难住了。谢谢!

编辑:感谢评论,我发现我收到了以下 cURL 错误:

Curl error: SSL certificate problem: unable to get local issuer certificate

【问题讨论】:

标签: php json api curl translate


【解决方案1】:

我正在寻找如何从 shell 中使用 API,但我在这里找到了示例代码:https://cloud.google.com/translate/docs/basic/translating-text

curl -HAuthorization:Bearer\ $(gcloud auth application-default print-access-token) -HContent-Type:application/json -d@&lt;(printf %s example|jq -sR '{q:.,target:"de"}') https://translation.googleapis.com/language/translate/v2|jq -r '.data.translations[0].translatedText'

【讨论】:

    猜你喜欢
    • 2017-06-25
    • 2016-08-09
    • 2023-03-09
    • 2011-06-06
    • 2011-12-26
    • 2010-10-10
    • 2018-07-24
    相关资源
    最近更新 更多