阿里 机器翻译 api[亲测有效]

(26) 2023-03-26 15:10

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说阿里 机器翻译 api[亲测有效],希望能够帮助你!!!。
阿里 机器翻译 api[亲测有效]_https://bianchenghao6.com/blog__第1张

申请阿里机器翻译服务 生成 akId, akSecret

阿狸机器翻译

composer 安装 alibabacloud/alimt 包

composer require alibabacloud/alimt

composer 安装 alibabacloud/client  包

composer require alibabacloud/client

 

<?php
namespace api\ali\translate;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
class ALiTrans
{
    public static function main($query, $from='auto', $to='zh')
    {
        AlibabaCloud::accessKeyClient('accessKeyId', 'accessKeySecret')
                                ->regionId('cn-hangzhou')
                                ->asDefaultClient();

        try {
            $result = AlibabaCloud::alimt()
                    ->v20181012()
                    ->translate()
                    ->withSourceText($query)
                    ->withSourceLanguage($from)
                    ->withTargetLanguage($to)
                    ->withFormatType("text")
                    ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            // print_r($e->getErrorMessage());
            trace($e->getErrorMessage(), 'info');
        } catch (ServerException $e) {
            // print_r($e->getErrorMessage());
            trace($e->getErrorMessage(), 'info');
        }
                                               
    }
}

 

上一篇

已是最后文章

下一篇

已是最新文章

发表回复