


Implementation method of connecting Baidu Wenxin Yiyan API with PHP to obtain specific types of sentences and translate them
How to implement PHP connection to Baidu Wenxin Yiyan API to obtain specific types of sentences and translate them
Overview:
In this article, we will learn how to use PHP connects to Baidu Wenxin Yiyan API, obtains specific types of sentences, and translates them through Baidu Translation API. This can give us a simple and fun way to get sentences and translate them. We will first understand the basic usage of Baidu Wenxin Yiyan API, and then use PHP to write code to implement this function.
Step 1: Apply for API key
First, we need to register on Baidu Open Platform and apply for Wenxinyiyan API key. Open the Baidu Open Platform website (https://openapi.baidu.com/), register and log in. Then create a new application and select Wenxinyiyan API. When applying for an API key, please make sure to set the "allow_baidu_translate" parameter to "true" so that we can use the Baidu Translate API to translate sentences.
Step 2: Connect to Baidu Wenxin Yiyan API
Use the following code snippet to connect to Baidu Wenxin Yiyan API and get a specific type of sentence:
<?php $apiKey = 'YOUR_API_KEY'; $type = 'TYPE_OF_SENTENCE'; // 可选择的句子类型: tangshi, songci, sanwen, lizhi, jingdian, gushi $apiUrl = "https://api.lovelive.tools/api/SweetNothings/$type"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $apiUrl, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Authorization: apikey ' . $apiKey ), )); $response = curl_exec($curl); curl_close($curl); if($response !== false) { $result = json_decode($response, true); $sentence = $result['data']['hitokoto']; } else { $sentence = 'Error: Failed to fetch sentence.'; } echo $sentence; ?>
Please make sure to add # Replace ##YOUR_API_KEY with the Baidu Wenxin Yiyan API key you applied for. You can also choose the type:
tangshi (Tang poetry),
songci (Song poetry),
sanwen (prose),
lizhi (inspirational),
jingdian (sentence),
gushi (story).
Next, we use Baidu Translation API to translate the obtained sentences. First, we need to register on Baidu Open Platform and apply for a translation API key. When requesting an API key, please make sure to set the "Access Control" item to "Open" in the application settings so that we can send requests through the API.
<?php $apiKey = 'YOUR_TRANSLATION_API_KEY'; $sentenceToTranslate = '我喜欢编程'; // 可替换成获取的句子 $apiUrl = "http://api.fanyi.baidu.com/api/trans/vip/translate"; $fromLang = "auto"; $toLang = "en"; $apiSalt = rand(10000, 99999); $apiSign = md5($apiKey . $sentenceToTranslate . $apiSalt . 'API_KEY_SECRET'); $translationUrl = "$apiUrl?q=" . urlencode($sentenceToTranslate) . "&from=$fromLang&to=$toLang&appid=$apiKey&salt=$apiSalt&sign=$apiSign"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $translationUrl, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ), )); $response = curl_exec($curl); curl_close($curl); if($response !== false) { $result = json_decode($response, true); $translatedSentence = $result['trans_result'][0]['dst']; } else { $translatedSentence = 'Error: Failed to translate sentence.'; } echo $translatedSentence; ?>
YOUR_TRANSLATION_API_KEY with the Baidu Translation API key you applied for. You can also change the source and target languages to other values as needed.
The above is the detailed content of Implementation method of connecting Baidu Wenxin Yiyan API with PHP to obtain specific types of sentences and translate them. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The edge browser comes with a translation function that allows users to translate anytime and anywhere, which brings great convenience to users. However, many users say that the built-in translation webpage is missing. Then the edge browser automatically What should I do if the translation page I brought is missing? Let this site introduce how to restore the translated web page that comes with the Edge browser if it is missing. How to restore the translation webpage that comes with the Edge browser is missing 1. Check whether the translation function is enabled: In the Edge browser, click the three dots icon in the upper right corner, and then select the "Settings" option. On the left side of the settings page, select the Language option. Make sure "Translate&rd"

How to connect the keep body fat scale? Keep has a specially designed body fat scale, but most users do not know how to connect the keep body fat scale. Next is the graphic tutorial on the connection method of the keep body fat scale that the editor brings to users. , interested users come and take a look! How to connect the keep body fat scale 1. First open the keep software, go to the main page, click [My] in the lower right corner, and select [Smart Hardware]; 2. Then on the My Smart Devices page, click the [Add Device] button in the middle; 3 , then select the device you want to add interface, select [Smart Body Fat/Weight Scale]; 4. Then on the device model selection page, click the [keep body fat scale] option; 5. Finally, in the interface shown below, finally [Add Now] at the bottom

According to news on July 22, today, the official Weibo of Xiaomi ThePaper OS announced that Xiaoai Translation has been upgraded. Real-time subtitles have been added to Japanese and Korean translations, and subtitle-free videos and live conferences can be transcribed and translated in real time. Face-to-face simultaneous interpretation supports translation into 12 languages, including Chinese, English, Japanese, Korean, Russian, Portuguese, Spanish, Italian, French, German, Indonesian, and Hindi. The above functions currently only support the following three new phones: Xiaomi MIX Fold 4 Xiaomi MIX Flip Redmi K70 Extreme Edition It is reported that in 2021, Xiao Ai’s AI subtitles will be added to Japanese and Korean translations. AI subtitles use Xiaomi’s self-developed simultaneous interpretation technology to provide a faster, more stable and accurate subtitle reading experience. 1. According to the official statement, Xiaoai Translator can not only be used in audio and video venues

When you use the Edge browser to access web pages, have you ever encountered a prompt that your connection is not a dedicated connection, causing web browsing to fail? How is this going? Many friends don’t know how to deal with this problem. You can take a look at the following three solutions. Method 1 (simple and crude): In the edge browser, you can try to solve the problem of the website being inaccessible by entering the settings and turning off the security function, and then blocking location permissions in the website permissions. It is important to note that the effectiveness and duration of this approach may vary, and specific effects cannot be determined. After restarting your browser, you can try visiting the website to see if the issue is resolved. Method 2: Adjust the keyboard to English input

Solutions to Restricted Network Connections in Win10 With the rapid development of technology, the Internet has become an indispensable part of people's lives. However, sometimes we may encounter some problems when connecting to the Internet on computers using the Windows 10 operating system, one of which is restricted connections. In this case, we cannot access web pages, download files, or use network functions normally. So, is there any way to solve this problem? This article will introduce you to several common solutions. 1. Check the network connection settings. First, I

With the development of the digital era, shared printers have become an indispensable part of the modern office environment. However, sometimes we may encounter the problem that the shared printer cannot be connected to the printer, which will not only affect work efficiency, but also cause a series of troubles. This article aims to explore the reasons and solutions for why a shared printer cannot connect to the printer. There are many reasons why a shared printer cannot connect to the printer, the most common of which is network issues. If the network connection between the shared printer and the printer is unstable or interrupted, normal operation will not be possible.

What does 0x0000011b mean when connecting to a printer? Users often encounter various error codes when using computers, laptops or other devices. Among them, 0x0000011b is a common printer connection error code. So, what does connecting printer 0x0000011b mean? First, we need to understand the basic principles of printer connection. When we need to print files from the computer, we usually need to connect the printer to the computer for data transfer between the two. This connection can be made via

1. Place the earphones in the earphone box and keep the lid open. Press and hold the button on the box to enter the pairing state of the earphones. 2. Turn on the watch music function and select Bluetooth headphones, or select Bluetooth headphones in the watch settings function. 3. Select the headset on the watch to pair successfully.
