jord-jd/php-wikipedia-search

PHP Wikipedia Search

Maintainers

Package info

github.com/Jord-JD/php-wikipedia-search

pkg:composer/jord-jd/php-wikipedia-search

Transparency log

Fund package maintenance!

DivineOmega

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 4

Open Issues: 0

v1.1.0 2026-07-18 04:57 UTC

This package is auto-updated.

Last update: 2026-07-18 04:58:37 UTC


README

Search Wikipedia editions through the current MediaWiki Action API and receive normalized, serializable result objects.

Installation

composer require jord-jd/php-wikipedia-search

Usage

use JordJD\WikipediaSearch\Enums\Languages;
use JordJD\WikipediaSearch\WikipediaSearcher;

$searcher = new WikipediaSearcher(Languages::ENGLISH);
$results = $searcher->search('PHP programming language', [
    'limit' => 25,
    'namespaces' => [0],
    'what' => 'text',
]);

foreach ($results as $result) {
    echo $result->getTitle();
    echo $result->getDescription();
    echo $result->getUrl();
    echo $result->getPageId();
    echo $result->getWordCount();
    echo $result->getTimestamp();
    echo json_encode($result);
}

The enum contains common Wikipedia language editions, but any valid Wikipedia language subdomain such as simple or zh-min-nan can be supplied.

Supported options follow MediaWiki's official list=search documentation: limit (1 to 500), offset, namespaces, and what (text, title, or nearmatch). Invalid values fail before an HTTP request is made.

Use getContinueOffset() as the next request's offset. The searcher also exposes getTotalHits(), getSuggestion(), and getRewrittenQuery() from the API response.

Custom HTTP transport

The optional second constructor argument is a callable receiving the URL, headers, and timeout. It must return the response body as a string:

$searcher = new WikipediaSearcher(
    Languages::ENGLISH,
    function (string $url, array $headers, int $timeout): string {
        return $yourHttpClient->get($url, $headers, $timeout);
    },
    10,
    'your-application/1.0 (https://example.com/contact)'
);

The package supports PHP 7.1 through current PHP 8.x releases.