阿里云机器翻译PHP使用Demo

SDK 安装

composer require alibabacloud/sdk

GitHub link

Code Sample

<?php
require __DIR__ . '/vendor/autoload.php';
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Download https://github.com/aliyun/openapi-sdk-php

AlibabaCloud::accessKeyClient('LTAIOZZ******', 'v7CjUJCMk7j9aKduMAQ**********')
->regionId('cn-hangzhou')
->asDefaultClient();


try {
    $result = AlibabaCloud::rpc()
    ->product('alimt')
    // ->scheme('https') // https | http
    ->version('2018-10-12')
    ->action('TranslateGeneral')
    ->method('POST')
    ->host('mt.cn-hangzhou.aliyuncs.com')
    ->options([
        'query' => [
            'RegionId' => "cn-hangzhou",
            'FormatType' => "text",
            'SourceLanguage' => "zh",
            'TargetLanguage' => "en",
            'SourceText' => "北京欢迎你",
            'Scene' => "general",
        ],
    ])
    ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}

The Result

Array ( [RequestId] => 908CA129-77EE-47E1-842B-B2B19FCFD492 [Data] => Array ( [Translated] => Welcome to Beijing ) [Code] => 200 ) 

参考链接

机器翻译通用版调用指南
阿里云常见参数获取位置

上一篇:阿里云新版人脸识别Java使用示例教程


下一篇:汇编语言基础教程-加法指令