jord-jd / php-wikipedia-search
PHP Wikipedia Search
Fund package maintenance!
Requires
- php: >=7.1
- jord-jd/php-base-search: ^4.1
Requires (Dev)
- phpunit/phpunit: ^7.5||^9.6||^10.5||^11.5||^12.5
Replaces
- divineomega/php-wikipedia-search: v1.1.0
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.