Home > Backend Development > PHP Tutorial > Sharing method of automatic translation from French to Chinese using PHP Baidu Translation API

Sharing method of automatic translation from French to Chinese using PHP Baidu Translation API

PHPz
Release: 2023-08-08 06:06:01
Original
1393 people have browsed it

Use PHP Baidu Translation API to achieve automatic translation method sharing from French to Chinese

Overview:
Baidu Translation API is a powerful machine translation service. By calling these API interfaces, we can achieve many Automatic translation between languages. This article will take French to Chinese translation as an example to introduce how to use PHP language to call Baidu Translation API to achieve automatic translation.

Step 1: Register a Baidu developer account and create a translation application
First, we need to register a Baidu developer account and create a translation application. Enter the Baidu Developer Center (http://developer.baidu.com/), click the "Register" button on the upper right, and follow the prompts to fill in the basic information to complete the registration. After successful registration, log in and create a new application in the console, select the "Translation" API, and record the assigned AppID, API Key and Secret Key.

Step 2: Obtain the access token
Before calling the Baidu Translation API, we need to authenticate through the access token (Access Token). Create a PHP file named "get_access_token.php" and copy the following code into the file:

<?php

$api_key = 'your_api_key';
$secret_key = 'your_secret_key';
$url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='.$api_key.'&client_secret='.$secret_key;

// 发起GET请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);

// 解析结果
$resultObj = json_decode($result, true);
$access_token = $resultObj['access_token'];

// 打印访问令牌
echo $access_token;

?>
Copy after login

Replace "your_api_key" and "your_secret_key" with those of the application you created in the Baidu Developer Center API Key and Secret Key. Save the file and run it, you will get an access token.

Step 3: Implement French to Chinese translation
Now, we can use the obtained access token to implement French to Chinese translation. Create a PHP file named "translate.php" and copy the following code into the file:

<?php

$access_token = 'your_access_token';
$api_url = 'https://fanyi-api.baidu.com/api/trans/vip/translate';

// 获取待翻译的文本
$text = $_POST['text'];

// 填写翻译源语言和目标语言
$from = 'fr';
$to = 'zh';

// 构建请求URL
$query_params = [
    'q' => $text,
    'from' => $from,
    'to' => $to,
    'appid' => 'your_app_id',
    'salt' => rand(10000, 99999)
];
$query_params['sign'] = md5($query_params['appid'].$text.$query_params['salt'].$access_token);

// 发起GET请求
$url = $api_url.'?'.http_build_query($query_params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);

// 解析结果
$resultObj = json_decode($result, true);
$translation = $resultObj['trans_result'][0]['dst'];

// 打印翻译结果
echo $translation;

?>
Copy after login

Replace "your_access_token" with the access token obtained in the previous step and "your_app_id" The AppID of the application you created in Baidu Developer Center. Save the file and place it in the same directory as "get_access_token.php".

In the HTML page, you can use the following form to enter the text that needs to be translated, and the translation results will be displayed on the page:

<!DOCTYPE html>
<html>
<head>
    <title>法语到中文翻译</title>
</head>
<body>
    <form action="translate.php" method="post">
        <input type="text" name="text" placeholder="输入法语文本">
        <button type="submit">翻译</button>
    </form>
    <div id="translation"></div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $("form").submit(function(e) {
            e.preventDefault();
            $.post($(this).attr("action"), $(this).serialize(), function(data) {
                $("#translation").text(data);
            });
        });
    </script>
</body>
</html>
Copy after login

Save and open this HTML page, you can Enter French text and click the "Translate" button to get the translation results.

Summary:
Through the above steps, we successfully implemented the automatic translation from French to Chinese using the PHP Baidu Translation API. You can further explore more features of Baidu Translation API and translation applications between other languages. Happy translating!

The above is the detailed content of Sharing method of automatic translation from French to Chinese using 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