All Products
Search
Document Center

Machine Translation:PHP SDK

Last Updated:Jul 19, 2023

Download link

The GitHub address of the Machine Translation SDK is as follows: SDK download

Procedure

To get started with the Machine Translation PHP SDK, perform the following steps:

Step 1 Create an Alibaba Cloud account

For more information, see Alibaba Cloud account registration process.in DBS.

We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2: Obtain an Alibaba Cloud AccessKey pair

To use the Machine Translation PHP SDK, you must apply for an AccessKey of Alibaba Cloud in DBS.

Log on to the KMS page. Select an AccessKey pair for the SDK. If no AccessKey pair is available, create one and make sure that it is in the Enable state. Keys refer to Access Key ID and Access Key Secret

The AccessKey pair will be used in the following steps and must be kept confidential.

Step 3 Start a new PHP project

You can start using the PHP SDK. Use any text editor or PHP IDE to run the following sample code to interact with the Machine Translation server and obtain the corresponding output.

Note

E-commerce version call DEMO example

<?php

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

// Create a global client.
AlibabaCloud::accessKeyClient('<your-access-key-id>', '<your-access-key-secret>')
            ->regionId('cn-hangzhou')
            ->asGlobalClient();

try {
    $result = AlibabaCloud::alimt()
                          ->v20181012() // E-commerce version
                          ->translateECommerce()
                          ->method('POST') // Set the request POST
                          ->withSourceLanguage('en') // source language
                          ->withScene('title') // Set the scenario. Product title: title, product description: description, and product communication: communication
                          ->withSourceText('book') // The original text.
                          ->withFormatType('text') // Format of the translated text
                          ->withTargetLanguage('zh') // target language
                          ->request();

    \print_r($result->toArray());

} catch (ServerException $e) {
    \print_r($e->getErrorMessage());
    \print_r($e->getRequestId());
} catch (ClientException $e) {
    \print_r($e->getErrorMessage());
}
Note

General Version Call DEMO Example

<?php

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

// Create a global client.
AlibabaCloud::accessKeyClient('<your-access-key-id>', '<your-access-key-secret>')
            ->regionId('cn-hangzhou')
            ->asGlobalClient();

try {
    $result = AlibabaCloud::alimt()
                          ->v20181012() // The general version.
                          ->translateGeneral()
                          ->method('POST') // Set the request POST
                          ->withSourceLanguage('en') // source language
                          ->withSourceText('book') // The original text.
                          ->withFormatType('text') // Format of the translated text
                          ->withTargetLanguage('zh') // target language
                          ->request();

    \print_r($result->toArray());

} catch (ServerException $e) {
    \print_r($e->getErrorMessage());
    \print_r($e->getRequestId());
} catch (ClientException $e) {
    \print_r($e->getErrorMessage());
}