Home > Backend Development > PHP Tutorial > How to achieve automatic translation from Spanish to Russian through PHP Baidu Translation API?

How to achieve automatic translation from Spanish to Russian through PHP Baidu Translation API?

WBOY
Release: 2023-08-06 14:14:01
Original
1320 people have browsed it

How to implement automatic translation from Spanish to Russian through PHP Baidu Translation API?

Introduction:
In today's era of globalization, cross-language translation has become an important demand. In the development field, the implementation of automated translation is a very challenging task. This article will introduce how to use PHP and Baidu Translation API to implement a simple but effective automatic translation example from Spanish to Russian.

1. Preparation:
First, we need to apply for a developer account on the Baidu Translation Open Platform and create a translation application. When creating the application, make sure that the Spanish to Russian translation language is turned on; after the creation is completed, we will obtain an API Key.

2. Install PHP extension:
In order to use Baidu Translation API, we need to install the curl extension in PHP. Curl can help us perform HTTP requests to interact with Baidu Translation API. You can install the curl extension in Linux through the following command:

sudo apt-get install php7.4-curl
Copy after login

3. Write PHP code:
The following is an example of PHP code to implement automatic translation from Spanish to Russian:

<?php
// 要翻译的文本
$text = "Hola, ¿cómo estás?";

// 百度翻译API的URL
$url = "http://api.fanyi.baidu.com/api/trans/vip/translate";

// 你的百度翻译API Key
$apiKey = "your_api_key";

// 你的百度翻译密钥
$secretKey = "your_secret_key";

// 生成sign参数的函数
function createSign($query, $salt, $appKey, $secretKey) {
    $str = $appKey . $query . $salt . $secretKey;
    $sign = md5($str);
    return $sign;
}

// 生成随机数salt的函数
function createSalt() {
    return rand(10000, 99999);
}

// 发起HTTP请求,并得到翻译结果的函数
function translate($query, $from, $to, $appKey, $secretKey) {
    $salt = createSalt();
    $sign = createSign($query, $salt, $appKey, $secretKey);
    $url = "http://api.fanyi.baidu.com/api/trans/vip/translate?q=".urlencode($query)."&from=".$from."&to=".$to."&appid=".$appKey."&salt=".$salt."&sign=".$sign;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($result, true);
    if ($result && isset($result['trans_result'][0]['dst'])) {
        return $result['trans_result'][0]['dst'];
    }
    return false;
}

// 调用翻译函数并打印结果
$translatedText = translate($text, "es", "ru", $apiKey, $secretKey);
if ($translatedText) {
    echo "翻译结果: " . $translatedText;
} else {
    echo "翻译失败";
}
?>
Copy after login

4. Run the code:
Save the above code as the translate.php file, and modify $apiKey and $secretKey to the API Key and Secret Key you applied for on the Baidu Translation Open Platform.

In the terminal, go into the directory where the code is located and run the following command:

php translate.php
Copy after login

If everything goes well, you will see the translation results.

Summary:
This article introduces how to use PHP and Baidu Translation API to implement an example of automatic translation from Spanish to Russian. By applying for a developer account, installing the curl extension and writing the corresponding PHP code, we can easily achieve cross-language translation. At the same time, this example can also be used as a basis for automatic translation of other language pairs. I hope this article can be helpful to everyone.

The above is the detailed content of How to achieve automatic translation from Spanish to Russian through PHP Baidu Translation API?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template