申请阿里机器翻译服务 生成 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');
}
}
}