Current Location: Home> Latest Articles> Implementing Full-Text Search and Keyword Filtering with Baidu Wenxin Yiyan API Using PHP

Implementing Full-Text Search and Keyword Filtering with Baidu Wenxin Yiyan API Using PHP

M66 2025-06-12

Implementing Full-Text Search and Keyword Filtering with Baidu Wenxin Yiyan API Using PHP

In web application development, integrating third-party APIs to extend functionality is a common requirement. This article will guide you through implementing the Baidu Wenxin Yiyan API with PHP to enable full-text search and keyword filtering.

Baidu Wenxin Yiyan API provides various categories of sentences, allowing us to search based on specific keywords and filter the returned results.

First, you need to apply for an API Key on the Baidu Developer platform. Once successfully applied, you can make HTTP requests to the Baidu Wenxin Yiyan API.

PHP Code Example: Calling the API Interface

Here is an example of calling the API using PHP and the cURL library:

<?php
// Set API Key
$apiKey = 'your_api_key';
// Set API URL
$url = 'http://api.mospeng.com/api/rand?key=' . $apiKey;

// Initialize cURL
$curl = curl_init();
// Set cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Send the request and get the response data
$response = curl_exec($curl);
// Close the cURL connection
curl_close($curl);
// Convert the response data to an array
$result = json_decode($response, true);
// Output the result
if ($result['code'] == 200) {
    echo $result['text'];
} else {
    echo 'Request failed';
}
?>
    

In the above code, we set the API Key and API URL, use cURL to send an HTTP GET request, and then process the API response. The response is converted into an array format, and the relevant sentence is displayed based on the result.

Implementing Full-Text Search and Keyword Filtering

Next, we will implement the functionality to search and filter based on user-input keywords. Assume that the user can submit a keyword via an input box, and here is the related code:

<?php
// Set API Key
$apiKey = 'your_api_key';
// Get the keyword input from the user
$keyword = $_GET['keyword'];
// URL encode the keyword
$keyword = urlencode($keyword);
// Set the API URL
$url = 'http://api.mospeng.com/api/search?key=' . $apiKey . '&keyword=' . $keyword;

// Initialize cURL
$curl = curl_init();
// Set cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Send the request and get the response data
$response = curl_exec($curl);
// Close the cURL connection
curl_close($curl);
// Convert the response data to an array
$result = json_decode($response, true);
// Output the result
if ($result['code'] == 200) {
    foreach ($result['data'] as $item) {
        echo $item['text'];
        echo '<br>';
    }
} else {
    echo 'Request failed';
}
?>
    

The above code demonstrates how to build the API URL based on the user's input keyword, send the HTTP request, and display the related sentences based on the keyword from the API response.

With the code examples above, you can easily integrate PHP with the Baidu Wenxin Yiyan API, allowing users to select from a variety of interesting sentences. This article provides a straightforward guide to implementing this feature in your web applications, and we hope it helps you in your project development.