Current Location: Home> Latest Articles> How to Use PHP to Connect to Baidu Wenxin Hitokoto API and Send Daily Quote to Users via Email

How to Use PHP to Connect to Baidu Wenxin Hitokoto API and Send Daily Quote to Users via Email

M66 2025-07-13

How to Use PHP to Connect to Baidu Wenxin Hitokoto API and Send Daily Quote to Users via Email

With the rapid development of the internet and mobile internet, users' demand for personalized services is increasing. As a website developer, providing unique and engaging content like a daily quote can attract more user attention and participation. This article will demonstrate how to use PHP to connect to the Baidu Wenxin Hitokoto API to fetch a daily quote and send it to users' email addresses.

Register Baidu Developer Account and Apply for API Access

First, you need to register a Baidu developer account and apply for access to the Baidu Wenxin Hitokoto API. Once your application is approved, you will receive an API Key, which you will use in the code that follows.

Write PHP Code to Connect to the API and Fetch the Daily Quote

Next, we will write the PHP code to connect to the Baidu Wenxin Hitokoto API and retrieve the daily quote. Here is a simplified example:

<?php
$url = "http://api.lwl12.com/hitokoto/main/get";
$params = [
    'key' => 'YOUR_API_KEY',
    'type' => 'json'
];

$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);

$data = json_decode($response, true);
$quote = $data['content'];

// Send the daily quote to the user's email
$to = 'user@example.com';
$subject = 'Daily Quote';
$message = $quote;
$headers = 'From: your_email@example.com' . "\r\n" .
           'Reply-To: your_email@example.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

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

Code Explanation

In the code above, we first define the URL for the Baidu Wenxin Hitokoto API and the parameters needed to call it. We then use the cURL library to send a GET request and retrieve the API's JSON response. After parsing the JSON, we extract the daily quote's content. Finally, we use PHP's mail() function to send the quote to the user's email.

Practical Use and Extensions

This example is simplified for illustration. In real-world applications, you would likely need to add error handling, data storage, and user authentication for more robust functionality. Also, since PHP's mail() function may not work properly on some servers, it's recommended to use a third-party email service to send emails.

Summary

By connecting to the Baidu Wenxin Hitokoto API, you can easily fetch a daily quote and automatically send it to users' emails. This not only provides personalized services for users but also helps increase user engagement on your website. If you have any questions during development, feel free to reach out. Wishing you success with your website development!