How to implement the machine translation function of Baidu Wenxin Yiyan's random sentences in PHP development?

WBOY
Release: 2023-08-12 15:14:01
Original
1547 people have browsed it

How to implement the machine translation function of Baidu Wenxin Yiyans random sentences in PHP development?

How to implement the machine translation function of Baidu Wenxin Yiyan random sentences in PHP development?

When developing a website or application, we often need to display some random sentences or famous quotes on the page, in order to better attract the user's attention and provide some enlightenment or entertainment. Baidu Wenxin Yiyan is a very popular open source project that provides a large number of random statements that can be obtained through API calls.

In this article, I will introduce to you how to use PHP to implement the machine translation function of Baidu Wenxinyiyan in order to support users in multiple languages.

First, we need to apply for an account on the Baidu AI open platform, create a machine translation instance, and obtain the API Key and Secret Key. This information will be used in our code.

Next, we need to use PHP to write a function to obtain random statements from Baidu Wenxinyiyan. Please refer to the following code example:

function getBaiduYiyan(){
    // 请求URL
    $url = "https://v1.hitokoto.cn/?c=d&encode=text";
    
    // 创建一个CURL会话
    $ch = curl_init();
    
    // 设置CURL参数
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    // 发起请求
    $response = curl_exec($ch);
    
    // 关闭CURL会话
    curl_close($ch);
    
    // 返回结果
    return $response;
}
Copy after login

In the above code, we use PHP's CURL function to send a GET request to obtain random statements from Baidu Wenxinyiyan. Here we use the API interface of hitokoto.cn and set the parameter "encode=text" to obtain random statements in text form.

Next, we need to use Baidu’s translation API to translate the obtained sentences. We also need to use the CURL function to send POST requests. Please refer to the following code example:

function translateText($text, $from, $to){
    // API地址
    $apiURL = "http://api.fanyi.baidu.com/api/trans/vip/translate";
    
    // API参数
    $appID = "your_app_id";
    $apiKey = "your_api_key";
    $secretKey = "your_secret_key";
    
    // 创建随机数
    $salt = rand(10000, 99999);
    
    // 计算签名
    $sign = md5($appID . $text . $salt . $secretKey);
    
    // 构造POST数据
    $postData = array(
        "q" => $text,
        "from" => $from,
        "to" => $to,
        "appid" => $appID,
        "salt" => $salt,
        "sign" => $sign
    );
    
    // 创建CURL会话
    $ch = curl_init();
    
    // 设置CURL参数
    curl_setopt($ch, CURLOPT_URL, $apiURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    
    // 发起请求
    $response = curl_exec($ch);
    
    // 关闭CURL会话
    curl_close($ch);
    
    // 解析JSON数据
    $result = json_decode($response, true);
    
    // 获取翻译结果
    $translation = $result["trans_result"][0]["dst"];
    
    // 返回结果
    return $translation;
}
Copy after login

In the above code, we use the HTTP interface of Baidu Translation API and need to set the API Key and Secret Key that we applied for on the Baidu AI Open Platform. We constructed a POST request, sent it to the API address, and translated the obtained random statements. Finally, we can get the translated results by accessing the $translation variable.

In our website or application, we can follow the following steps to call these functions to obtain and display random statements:

// 获取随机语句
$randomQuote = getBaiduYiyan();

// 翻译语句
$translatedQuote = translateText($randomQuote, "auto", "en");

// 输出随机语句和翻译结果
echo "原语句:" . $randomQuote;
echo "翻译结果:" . $translatedQuote;
Copy after login

In the above code, we first use the getBaiduYiyan() function to obtain A random original statement, which is then passed to the translateText() function for translation. Finally, we output the original sentences and translation results to the page.

Through the above code examples, we can easily implement the machine translation function of Baidu Wenxin Yiyan's random sentences. Whether used for website or application development, such features can bring a richer experience to users and provide more inspiration or inspiration.

The above is the detailed content of How to implement the machine translation function of Baidu Wenxin Yiyan's random sentences in PHP development?. 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