PHP code example of freearhey / wikidata
1. Go to this page and download the library: Download freearhey/wikidata library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
freearhey / wikidata example snippets
use Wikidata\Wikidata;
$wikidata = new Wikidata();
$results = $wikidata->search($query, $lang, $limit);
$results = $wikidata->search('car', 'fr', 5);
/*
Collection {
#items: array:5 [
0 => SearchResult {
id: "Q1759802"
lang: "fr"
label: "autocar"
wiki_url: "https://fr.wikipedia.org/wiki/autocar"
description: "transport routier pouvant accueillir plusieurs voyageurs pour de longues distances"
aliases: array:1 [
0 => "car"
]
}
1 => SearchResult {
id: "Q224743"
lang: "fr"
label: "Car"
wiki_url: "https://fr.wikipedia.org/wiki/Car"
description: "page d'homonymie d'un projet Wikimédia"
aliases: []
}
...
]
}
*/
$results = $wikidata->searchBy($propId, $entityId, $lang, $limit);
// List of people who born in city Pomona, US
$results = $wikidata->searchBy('P19', 'Q486868');
/*
Collection {
#items: array:10 [
0 => SearchResult {
id: "Q92638"
lang: "en"
label: "Robert Tarjan"
wiki_url: "https://en.wikipedia.org/wiki/Robert_Tarjan"
description: "American computer scientist"
aliases: array:2 [
0 => "Robert E. Tarjan"
1 => "Robert Endre Tarjan"
]
}
1 => SearchResult {
id: "Q184805"
lang: "en"
label: "Tom Waits"
wiki_url: "https://en.wikipedia.org/wiki/Tom_Waits"
description: "American singer-songwriter and actor"
aliases: []
}
...
]
}
*/
$entity = $wikidata->get($entityId, $lang);
// Get all data about Steve Jobs
$entity = $wikidata->get('Q19837');
/*
Entity {
id: "Q19837"
lang: "en"
label: "Steve Jobs"
wiki_url: "https://en.wikipedia.org/wiki/Steve_Jobs"
aliases: array:2 [
0 => "Steven Jobs",
1 => "Steven Paul Jobs"
]
description: "American entrepreneur and co-founder of Apple Inc."
properties: Collection { ... }
}
*/
// List of all properties as an array
$properties = $entity->properties->toArray();
/*
[
"P1006" => Property {
id: "P1006"
label: "NTA ID"
values: Collection {
#items: array:6 [
0 => Value {
id: "Q5593916"
label: "Grammy Trustees Award"
qualifiers: Collection {
#items: array:1 [
0 => Qualifier {
id: "P585"
label: "point in time"
value: "2012-01-01T00:00:00Z"
}
]
}
},
...
]
}
},
...
]
*/