Home > Backend Development > PHP Tutorial > How to connect Baidu Wenxin Yiyan API with PHP to obtain daily sentences and send them to the user's mailbox

How to connect Baidu Wenxin Yiyan API with PHP to obtain daily sentences and send them to the user's mailbox

WBOY
Release: 2023-08-12 09:02:01
Original
1323 people have browsed it

How to connect Baidu Wenxin Yiyan API with PHP to obtain daily sentences and send them to the users mailbox

How to use PHP to connect to Baidu Wenxin Yiyan API to obtain a daily sentence and send it to the user's mailbox

With the rapid development of the Internet and mobile Internet, users are more interested in personality The demand for specialized services is also increasing. As a website developer, if you can provide users with unique and exciting text content in one sentence every day, you will attract more user attention and participation. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API to obtain a daily sentence and send it to the user's mailbox.

First, we need to register a Baidu developer account and apply for access to Baidu Wenxin Yiyan API. After the application is successful, we will obtain an API Key, which will be used in subsequent code.

Next, we need to write PHP code to connect to Baidu Wenxin Yiyan API and get the daily sentence. The following is a summary of a sample code:

<?php
// 百度文心一言API的URL
$url = "http://api.lwl12.com/hitokoto/main/get";

// 需要传递给API的参数,包括API Key和返回的数据格式
$params = [
    'key' => 'YOUR_API_KEY',
    'type' => 'json'
];

// 使用cURL库发送GET请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

// 解析API返回的JSON数据
$data = json_decode($response, true);

// 获取每日一句的内容
$quote = $data['content'];

// 将每日一句存储到文件或数据库中,供后续使用
// ...

// 发送邮件到用户邮箱
$to = 'user@example.com';
$subject = '每日一句';
$message = $quote;
$headers = 'From: your_email@example.com' . "
" .
    'Reply-To: your_email@example.com' . "
" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
Copy after login

In the above code, we first define the URL of Baidu Wenxin Yiyan API and the parameters that need to be passed to the API. We then use the cURL library to send a GET request to get the JSON data returned by the API. After parsing the JSON data, you can get the content of the daily sentence. Finally, we use PHP's mail() function to send the daily sentence to the user's mailbox. You need to replace YOUR_API_KEY with your own API Key and user@example.com with the user’s email address.

Of course, the above code is just a simplified example. In actual development, you also need to add functions such as error handling, data storage, and user verification. At the same time, since PHP's mail() function may not run properly on some servers, you may need to consider using a third-party email sending service.

To summarize, by connecting to Baidu Wenxin Yiyan API, we can easily obtain a daily sentence and send it to the user's mailbox. This not only provides users with personalized services, but also helps increase the user stickiness of the website. Hope this article can be helpful to you. If you have any questions, you can contact me anytime. Good luck with your website development!

The above is the detailed content of How to connect Baidu Wenxin Yiyan API with PHP to obtain daily sentences and send them to the user's mailbox. 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