Home >Backend Development >PHP Tutorial >How to realize mutual translation between Chinese and English using PHP Baidu Translation API
How to use PHP Baidu Translation API to achieve mutual translation between Chinese and English
Overview:
In actual development, we often need to realize the function of mutual translation between Chinese and English, and Baidu Translation API provides a Easy way to achieve this functionality. This article will introduce how to use PHP and Baidu Translation API to achieve mutual translation between Chinese and English, and attach code examples.
Steps:
The following are the specific steps to achieve mutual translation between Chinese and English:
<?php // 1. 设置百度翻译API的接口信息 $apiUrl = 'https://fanyi-api.baidu.com/api/trans/vip/translate'; $appId = 'your_app_id'; // 替换成你的API Key $secretKey = 'your_secret_key'; // 替换成你的Secret Key // 2. 构建HTTP请求的URL $q = 'hello'; // 待翻译的文本 $from = 'en'; // 源语言 $to = 'zh'; // 目标语言 $salt = mt_rand(); // 随机数 $sign = md5($appId . $q . $salt . $secretKey); // 签名 $url = $apiUrl . '?q=' . urlencode($q) . '&from=' . $from . '&to=' . $to . '&appid=' . $appId . '&salt=' . $salt . '&sign=' . $sign; // 3. 发送HTTP请求并获取结果 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); // 4. 解析结果并输出翻译结果 $data = json_decode($result, true); if ($data['error_code'] == 0) { echo $data['trans_result'][0]['dst']; // 输出翻译结果 } else { echo '翻译失败:' . $data['error_msg']; } ?>
In the above code, you need to replace your_app_id
and your_secret_key
with the ones you translated in Baidu The API Key and Secret Key obtained when creating an application on the open platform. $q
is the text to be translated, $from
is the source language, $to
is the target language, in this example, we translate English into Chinese.
<?php function translate($text, $from, $to) { // 设置百度翻译API的接口信息 $apiUrl = 'https://fanyi-api.baidu.com/api/trans/vip/translate'; $appId = 'your_app_id'; // 替换成你的API Key $secretKey = 'your_secret_key'; // 替换成你的Secret Key // 构建HTTP请求的URL $salt = mt_rand(); // 随机数 $sign = md5($appId . $text . $salt . $secretKey); // 签名 $url = $apiUrl . '?q=' . urlencode($text) . '&from=' . $from . '&to=' . $to . '&appid=' . $appId . '&salt=' . $salt . '&sign=' . $sign; // 发送HTTP请求并获取结果 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); // 解析结果并返回翻译结果 $data = json_decode($result, true); if ($data['error_code'] == 0) { return $data['trans_result'][0]['dst']; // 返回翻译结果 } else { return '翻译失败:' . $data['error_msg']; } } // 测试中英互相翻译 $text = 'hello'; // 待翻译的文本 $from = 'en'; // 源语言 $to = 'zh'; // 目标语言 $translatedText = translate($text, $from, $to); echo '英文翻译成中文:' . $translatedText . " "; $translatedText = translate($translatedText, $to, $from); echo '中文翻译成英文:' . $translatedText . " "; ?>
The above code will output the results of English translation into Chinese, and then the results of translation from Chinese into English.
Summary:
With the help of Baidu Translation API and PHP, we can realize the function of mutual translation between Chinese and English. With the method described in this article, you can easily integrate this functionality into your project and extend it as needed. Hope this article is helpful to you!
The above is the detailed content of How to realize mutual translation between Chinese and English using PHP Baidu Translation API. For more information, please follow other related articles on the PHP Chinese website!