When developing websites, developers often need to use open APIs to fetch external data. The Baidu Wenxin Hitokoto API is a popular free interface that provides various beautiful quotes, suitable for use on "quote" websites. In this article, we will explore how to connect to the Baidu Wenxin Hitokoto API using PHP, retrieve specific types of quotes, and showcase several effective filtering methods to better meet the development requirements.
The Baidu Wenxin Hitokoto API is a completely free public interface that provides various types of quotes, including famous sayings, motivational quotes, anime lines, game quotes, etc., making it suitable for website content display. By accessing this API, developers can retrieve different types of sentences:
The API does not require any authentication information; a simple GET request will return the data. For example, you can use PHP’s file_get_contents
In the above code, the URL parameter ?c=a specifies that the quote type is "anime." Other optional parameters include: c=c for "ancient style," and c=d for "chicken soup" type.
The returned data is a PHP associative array that contains the quote, author, source, and other information. You can retrieve the quote and author like this:
$hitokoto = $result['hitokoto'];
$author = $result['from'];
The above method fetches generic quotes, but if we need specific types of sentences, we can add parameters to the URL to filter the data. For example, to retrieve "programming" quotes in Chinese text format, you can use the following:
$url = "https://v1.hitokoto.cn/?c=program&encode=text&charset=utf-8";
$response = file_get_contents($url);
In this case, the c=program parameter is used to fetch "programming" quotes, and the format is set to text encode=text, with UTF-8 character set charset=utf-8.
To ensure that the website content does not contain inappropriate language, developers can use regular expressions to filter out offensive words. For example, the following code filters out common profanities:
$badwords = array('草', '操', '尼玛', '妈逼');
$hitokoto = preg_replace('/' . implode('|', $badwords) . '/i', '**', $hitokoto);
The above code uses PHP’s preg_replace function, combining the bad words into a regular expression using the pipe symbol (OR operator |), ignoring case /i, and replacing them with stars **.
To maintain a clean page layout, developers often need to limit the length of sentences to prevent long sentences from breaking the design. The following code ensures the sentence does not exceed a specified length:
$hitokoto = mb_substr($hitokoto, 0, 20);
In this code, the PHP function mb_substr is used to limit the sentence length to 20 characters, starting from the 0th character.
To ensure the user input is safe, developers need to remove HTML tags from the sentence. The following PHP code does this using the strip_tags function:
$hitokoto = strip_tags($hitokoto);
This function removes all HTML tags from the string and returns the plain text content, preventing users from injecting malicious code into the site.
This article introduced how to use PHP to connect to the Baidu Wenxin Hitokoto API, retrieve specific types of sentences, and process the data using methods such as regular expressions, character length control, and HTML tag filtering. These techniques help ensure that the content displayed on the website meets the requirements while maintaining good quality and security. Developers can further customize and extend these methods based on their specific needs.