How to realize automatic translation from Korean to German through PHP Baidu Translation API?

王林
Release: 2023-08-05 16:48:01
Original
1489 people have browsed it

How to realize automatic translation from Korean to German through PHP Baidu Translation API?

In today's era of globalization, cross-border exchanges have become more and more frequent. Language barriers between different countries have become one of the main barriers to communication. In order to solve this problem, people began to look for various automatic translation tools. In this article, I will introduce to you how to use the PHP Baidu Translation API to achieve automatic translation from Korean to German.

First, we need to register a Baidu Translate developer account and create an application to obtain the API key. Once created, we can start using the API for automatic translation.

Next, we need to introduce the library file of Baidu Translation API into the PHP file. The corresponding library files and sample codes can be found on the documentation page of the Baidu Translation Developer Center. Download the library file and place it in the appropriate location of the project, and introduce it in the PHP file.

In the code, we need to define some necessary parameters. The first is the address and key of Baidu Translation API:

$apiUrl = 'http://api.fanyi.baidu.com/api/trans/vip/translate';
$appId = '您的应用ID';
$apiKey = '您的密钥';
$fromLang = 'ko'; // 韩语
$toLang = 'de'; // 德语
Copy after login

Next, we need to obtain the text content to be translated. The input Korean text can be obtained from the front-end page through GET or POST.

$originText = $_POST['text']; // 韩语文本
Copy after login

Then, we need to construct a URL to send an HTTP request and pass the Korean text as a parameter to the API. At the same time, other necessary parameters and keys also need to be added to the URL.

$apiParams = [
    'q' => $originText,
    'appid' => $appId,
    'salt' => uniqid(), // 生成一个唯一的随机字符串作为salt
    'from' => $fromLang,
    'to' => $toLang,
];
$apiParams['sign'] = md5($appId . $originText . $apiParams['salt'] . $apiKey);
$apiUrl .= '?' . http_build_query($apiParams);
Copy after login

Now, we can use cURL to send HTTP requests and get the results returned by the API.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
Copy after login

Finally, we can parse the results returned by the API, obtain the translated German text and output it.

$result = json_decode($response, true);
$translatedText = $result['trans_result'][0]['dst']; // 获取翻译结果
echo $translatedText;
Copy after login

So far, we have completed the code example of automatic translation from Korean to German using PHP. You can enter the Korean text in the above code into a specific HTML page and trigger the translation function through form submission or other methods.

It should be noted that Baidu Translation API has a monthly translation request limit for free developer accounts. If you exceed the limit, you may need to upgrade to a paid account to get more API calls.

In summary, it is not difficult to realize automatic translation from Korean to German through the PHP Baidu Translation API. Just get the API key, introduce the library file, build the URL to send the request, and parse the API to return the result. I hope this article can be helpful to you, and I wish you good luck in achieving it!

The above is the detailed content of How to realize automatic translation from Korean to German 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!