Wikidata provides a API for searching and retrieving data from wikidata.org.
composer require freearhey/wikidata
$wikidata = new Wikidata();
Search by entity label:
$results = $wikidata->search('London');
Search by Wikidata property ID and string value:
$results = $wikidata->searchBy('P238', 'LON');
Search by Wikidata property ID and entity ID:
$results = $wikidata->searchBy('P17', 'Q146');
Check if no search results
if($results->isEmpty()) {
echo 'no results';
die();
}
Retrieve first result data
$singleResult = $results->first();
Get result ID
$resultId = $singleResult->id; // Q84
Get result label
$resultLabel = $singleResult->label; // London
Get result aliases
$resultAliases = $singleResult->aliases; // [ 'London, UK', 'London, United Kingdom', 'London, England' ]
Get result description
$resultDescription = $singleResult->description; // capital of England and the United Kingdom
Get single entity by ID:
$entity = $wikidata->get('Q26');
Get entity ID
$entityId = $entity->id; // Q26
Get entity label
$entityLabel = $entity->label; // Northern Ireland
Get entity aliases
$entityAliases = $entity->aliases; // [ 'NIR', 'UKN', 'North Ireland' ]
Get entity description
$entityDescription = $entity->description; // region in north-west Europe, part of the United Kingdom
Get list of all available properties for specific entity
$properties = $entity->properties; // array(1) { [0]=> string(11) "instance_of", ... }
Get value specific property
$official_language = $entity->get('official_language'); // array(1) { [0]=> string(7) "English" }
That's all.
Wikidata is licensed under the MIT license.