Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说阿里 机器翻译 api[亲测有效],希望能够帮助你!!!。
阿狸机器翻译
composer require alibabacloud/alimt
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');
}
}
}
上一篇
已是最后文章
下一篇
已是最新文章