代码之家  ›  专栏  ›  技术社区  ›  Paddy Hallihan

使用google translate api时出错

  •  0
  • Paddy Hallihan  · 技术社区  · 6 年前

    我刚开始学习谷歌翻译API,我想弄清楚如何正确地设置它。

    我先去了 https://console.developers.google.com 在这里,我设置了账单,创建了一个项目,进入“凭证”并生成了一个api密钥。

    然后我转到命令提示符,按照下面描述的步骤通过composer安装。我不得不用 composer require google/cloud 因为 composer require google/cloud-translation 出了个错误。

    安装的很好,我现在可以看到目录 C:\wamp64\www\myproject\vendor\google\cloud\Translate 是的。

    然后我尝试创建一个简单的脚本,比如:

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

    但我得到了以下错误消息:

    ( ! ) 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
    

    就像我说的,我对这件事很了解,所以我不知道我是不是做得对,或者我错过了一些对某些人来说很明显的东西。

    如有任何帮助或建议,将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Christopher P    6 年前

    所以我知道这是显而易见的…

    错误说明: SSL certificate problem 是的。

    我的目录是: C:\wamp64\www\myproject

    我在wamp上有一个alpha版本的站点,它没有ssl证书,然后是beta版本和live版本,它们都有证书。

    google需要ssl证书,所以这个脚本在我的beta子目录中工作,但在wamp上不在本地工作。