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'; // 德语
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']; // 韩语文本
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);
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);
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;
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!