Search code examples
phpgoogle-translategoogle-translation-api

Errors when using the Google Translate API


I am just starting to learn the Google Translate API and am trying to figure out how to get it set up properly.

First I went to https://console.developers.google.com where I set up billing, created a project, went into 'Credentials' and generated an API key.

I then went to Command Prompt and followed the steps to install via composer as described here. I had to use composer require google/cloud because composer require google/cloud-translation gave an error.

This installed fine and I can now see the directory C:\wamp64\www\myproject\vendor\google\cloud\Translate.

I then tried to create a simple script like so:

<?php

require 'vendor/autoload.php';
use Google\Cloud\Translate\TranslateClient;
$translate = new TranslateClient([
    'key' => '{my API key}'
]);

if(isset($_POST["string"])){
    $string = $_POST["string"];
    $result = $translate->translate($string, [
        'target' => 'fr'
    ]);
    echo $result['text'] . "\n";
}

?>

<html>
<head>
</head>
<body>
<form action="test_translate.php" method="POST">
<input type="text" name="string">
<input type="submit">
</form>
</body>
</html>

But I get the following error messages:

( ! ) Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' with message 'cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in C:\wamp64\www\Lifting365\vendor\google\cloud\Core\src\RequestWrapper.php on line 263
( ! ) Google\Cloud\Core\Exception\ServiceException: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in C:\wamp64\www\Lifting365\vendor\google\cloud\Core\src\RequestWrapper.php on line 263
Call Stack
#   Time    Memory  Function    Location
1   0.0007  242936  {main}( )   ...\test_translate.php:0
2   0.0489  1920632 Google\Cloud\Translate\TranslateClient->translate( )    ...\test_translate.php:13
3   0.0489  1921032 Google\Cloud\Translate\TranslateClient->translateBatch( )   ...\TranslateClient.php:181
4   0.0489  1921536 Google\Cloud\Translate\Connection\Rest->listTranslations( ) ...\TranslateClient.php:246
5   0.0489  1921848 Google\Cloud\Translate\Connection\Rest->send( ) ...\Rest.php:78
6   0.1728  2338192 Google\Cloud\Core\RequestWrapper->send( )   ...\RestTrait.php:96

I'm new to learning this so I'm not sure if I'm doing this right or if I'm missing something.


Solution

  • So I knew it was something glaringly obvious....

    The error stated: SSL certificate problem.

    And my directory was: C:\wamp64\www\myproject

    I have an Alpha version of my site on WAMP, which doesn't have an SSL certificate, and then a Beta version and a live version, which both do have certificates.

    Google requires an SSL cert, so the script works in my beta subdirectory but not locally on WAMP.