使用谷歌翻译API与cURL

时间:2016-01-26 12:06:14

标签: php json api curl translate

我正在尝试使用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

1 个答案:

答案 0 :(得分:0)