Current Location: Home> Latest Articles> Implementing Xinhua Dictionary Lookup with PHP: API and HTML Parsing Tutorial

Implementing Xinhua Dictionary Lookup with PHP: API and HTML Parsing Tutorial

gitbox 2025-07-27

How to Query Xinhua Dictionary with PHP

In PHP, we can query the Xinhua Dictionary either by calling an API or by parsing HTML pages. This article provides a detailed explanation of both methods to help developers implement convenient query functions.

Querying via API

API Overview

Xinhua Dictionary offers an API interface that allows developers to implement dictionary lookup functionality. The detailed API documentation can be found on the official Xinhua Dictionary website.

API Query Example

First, you need to obtain an API Key, which is used for authentication. Then, use PHP’s cURL library to send an HTTP request to the Xinhua Dictionary API endpoint, retrieve the response, and parse the results.


$apiKey = 'your_api_key';
$keyword = 'keyword_to_search';
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.xinhua.io/dictionary/v1/entries?api_key=' . $apiKey . '&keyword=' . urlencode($keyword),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json'
    ),
));
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response, true);

In the code above, we use PHP’s curl_init() function to initialize a cURL session and set options such as the request URL, timeout, and HTTP headers. Then we execute the request and get the response data.

The returned data is a JSON string, which we convert into a PHP associative array using json_decode(), making it easier to process.

Querying via HTML Parsing

Analyzing Page Structure

The online query page of the Xinhua Dictionary can also be accessed by parsing HTML. To achieve this, we need to inspect the HTML structure of the page and locate the elements containing the query results.

First, open the Xinhua Dictionary query page, use developer tools to analyze the elements on the page, and identify the HTML nodes containing the results.

HTML Parsing Example with a Library

In PHP, you can use third-party libraries such as Simple HTML DOM to parse HTML pages. First, include the Simple HTML DOM library file.


require 'simple_html_dom.php';

Then, use the file_get_html() function to load HTML code from a URL or file, and the find() method to locate the required elements.


$html = file_get_html('http://www.xinhua.io/dictionary');
$result = $html->find('.result');
foreach ($result as $word) {
    $definition = $word->find('.definition', 0)->innertext;
    echo $definition;
}

In this example, we load the Xinhua Dictionary query page with file_get_html(), use find() to locate elements with the result class, iterate through each query result, extract the definition content, and print it out.

Conclusion

This article introduced two approaches to implementing Xinhua Dictionary queries: API calls and HTML parsing. Developers can choose the method that best fits their needs to easily integrate dictionary lookup functionality.