2

I'm trying to use the Google Translate API to translate text input by the user on my php based website. So far I have:

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

This isn't returning anything and I don't know what's wrong. I know that the API is set up correctly as when I enter the url into a browser it returns the following:

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

It must be something to do with my code which I took from this This Site

Any help would be appreciated as I'm completely stumped. Thanks!

EDIT: thanks to a comment I was able to find out that I'm getting the following cURL error:

Curl error: SSL certificate problem: unable to get local issuer certificate
Angus Allman
  • 35
  • 1
  • 4
  • Did you get any cURL errors? Know more about cURL errors here: http://php.net/manual/en/function.curl-error.php – ash__939 Jan 26 '16 at 12:28
  • thanks for this! I just found out I'm getting this error in cURL `Curl error: SSL certificate problem: unable to get local issuer certificate` – Angus Allman Jan 26 '16 at 12:57
  • This question has an answer for this error. http://stackoverflow.com/questions/21114371/php-curl-error-code-60 – ash__939 Jan 27 '16 at 11:28

1 Answers1

0

I was searching for how to use the API from a shell, but I found example code here: https://cloud.google.com/translate/docs/basic/translating-text.

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

nisetama
  • 7,764
  • 1
  • 34
  • 21