How to use PHP Baidu Translation API to translate from German to Chinese?

王林
Release: 2023-08-09 09:02:02
Original
1490 people have browsed it

How to use PHP Baidu Translation API to translate from German to Chinese?

How to use PHP Baidu Translation API to translate German to Chinese?

Introduction:
In today's era of globalization, language communication has become an important issue. When we encounter a language we don’t understand, we can use translation tools to solve this problem. Baidu Translation API is a powerful tool that can help us achieve translation in multiple languages. This article will introduce how to use the PHP Baidu Translation API to achieve German to Chinese translation.

Preparation work:
First, we need to register a Baidu Translation developer account and apply for an API Key. After the registration is completed, we can obtain the API Key and Secret Key. This information will be used in subsequent code examples.

Step 1: Install the necessary components
First, we need to make sure that PHP, as well as curl and openssl extensions are installed on our server. You can check whether these components have been installed by running the following command:

php -m | grep -i curl
php -m | grep -i openssl
Copy after login

If no output is displayed, it means that the corresponding components are not installed. These components can be installed through the following command:

sudo apt-get install php-curl
sudo apt-get install php-openssl
Copy after login

Step 2: Write PHP script
Next, we create a PHP file, such as translate.php, and write the following code:

<?php
function translate($query) {
    $app_id = 'your_app_id'; // 在此替换成你的App ID
    $api_key = 'your_api_key'; // 在此替换成你的API Key
    $secret_key = 'your_secret_key'; // 在此替换成你的Secret Key

    $salt = rand(10000, 99999);
    $sign = md5($app_id.$query.$salt.$secret_key);

    $url = 'http://api.fanyi.baidu.com/api/trans/vip/translate';
    $params = [
        'q' => $query,
        'from' => 'de',
        'to' => 'zh',
        'appid' => $app_id,
        'salt' => $salt,
        'sign' => $sign,
    ];

    $url = $url . '?' . http_build_query($params);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $result = curl_exec($ch);
    curl_close($ch);

    $result = json_decode($result, true);
    return $result['trans_result'][0]['dst'];
}

$input = 'Hallo Welt'; // 德语输入
$output = translate($input); // 翻译后的中文
echo '输入:' . $input . PHP_EOL;
echo '输出:' . $output . PHP_EOL;
?>
Copy after login

In the above code, we first define the translate function for actual translation operations. Inside the function, we first obtain the API Key and Secret Key, and generate a salt value through a random number. Then, we generate a signature based on the request parameters. Next, we build a complete API request URL and send the request using the curl library. Finally, we parse the JSON result returned by the API and return the translated text.

In the sample code, we define an input string Hallo Welt, and then call the translate function to perform the translation operation and output the result.

Step 3: Run the code
Save the above PHP script. We can run this script through the command line or browser. In the command line, we can run it through the following command:

php translate.php
Copy after login

Or, enter the URL address of the script in the browser, such as http://localhost/translate.php to run it script.

After running the code, we can see the following output:

输入:Hallo Welt
输出:你好世界
Copy after login

This is the result of using the PHP Baidu Translation API to achieve German to Chinese translation.

Summary:
Through the above steps, we successfully used the PHP Baidu Translation API to achieve German to Chinese translation. I hope this article will be helpful to you and make it easier for you to solve language communication problems. If you need translation into other languages, just modify the parameters in the code. Thank you for reading!

The above is the detailed content of How to use PHP Baidu Translation API to translate from German to Chinese?. 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