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
3

1 Answer 1

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'

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.