Home > Article > Backend Development > 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; ?>Please make sure to replace
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!