In development, we often need to call third-party APIs to fetch data. Baidu Wenxin Yiyan is a popular API that provides many famous quotes, which can add fun and philosophical elements to a webpage. This article will introduce how to use PHP to implement full-text search and keyword filtering features via the Baidu Wenxin Yiyan API.
First, we need to obtain the API interface from the official Baidu Wenxin Yiyan website. You can search for "Baidu Wenxin Yiyan API" on Baidu, find the API documentation, and learn how to use the relevant interface.
According to the documentation, we can use the curl library in PHP to make API calls. The API interface typically requires us to pass certain parameters and will return data in a specific format. In this article, we will demonstrate how to implement full-text search and keyword filtering as examples.
The full-text search feature helps us quickly find Wenxin Yiyan that matches certain criteria. First, we need to use the curl library to send a request. Here is an example of PHP code:
$keyword = "love"; // Search keyword
$url = "https://api.example.com/search?keyword=" . $keyword;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if ($data["code"] == 200) {
foreach ($data["data"] as $item) {
echo "<p>{$item['content']}</p>";
}
} else {
echo "<p>{$data['message']}</p>";
}
In the code above, we first define a search keyword, $keyword. Then, we construct the API request URL, passing the keyword as a parameter. Next, we use the curl library to send the request and store the response in the $response variable.
Using the json_decode function, we decode the returned JSON data into an associative array, $data. Based on the data format returned by the API, we check if the returned code is 200. If successful, we iterate through the $data["data"] array and output each piece of content; otherwise, we output an error message.
The keyword filtering feature helps us filter out unwanted Wenxin Yiyan and only display the relevant content. Here is an example of PHP code:
$keyword = "love"; // Filter keyword
$url = "https://api.example.com/get_all";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if ($data["code"] == 200) {
foreach ($data["data"] as $item) {
if (strpos($item["content"], $keyword) === false) {
echo "<p>{$item['content']}</p>";
}
}
} else {
echo "<p>{$data['message']}</p>";
}
In this code, we define a filter keyword, $keyword. Then, we construct the API request URL and use the get_all interface to fetch all Wenxin Yiyan data. We use the curl library to send the request and save the result in the $response variable.
Next, we decode the returned JSON data into an associative array, $data, using the json_decode function. Based on the API's returned data format, we check if the returned code is 200. If successful, we iterate through the $data["data"] array and use the strpos function to check if each piece of content contains the filter keyword. If it does not, we output that content; otherwise, we skip it.
This article introduced how to use PHP to implement Baidu Wenxin Yiyan API full-text search and keyword filtering functions. By using the curl library to send API requests and parse the returned JSON data, we can easily retrieve Wenxin Yiyan content and perform full-text searches and keyword filtering based on our actual needs.
By implementing these features, websites can add philosophical quotes, enhancing user interaction and the website's appeal.