简体   繁体   中英

Using google translate API with cURL

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM