freearhey / wikidata
用于处理Wikidata API的PHP客户端。
3.6.0
2020-12-20 10:12 UTC
Requires
- guzzlehttp/guzzle: ^7.0
- tightenco/collect: ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Wikidata 
Wikidata提供了一个API,用于搜索和检索来自wikidata.org的数据。
安装
composer require freearhey/wikidata
使用方法
首先,我们需要创建一个Wikidata类的实例并将其保存到某个变量中,如下所示
require_once('vendor/autoload.php'); use Wikidata\Wikidata; $wikidata = new Wikidata();
然后我们可以使用可用的方法之一来访问Wikidata数据库。
可用方法
search()
search()方法提供了一种通过其标签找到Wikidata实体的方式。
$results = $wikidata->search($query, $lang, $limit);
参数
$query: 要搜索的术语(必需)$lang: 指定结果语言(默认:'en')$limit: 设置自定义限制(默认:10)
示例
$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: [] } ... ] } */
search()方法始终返回包含结果的Illuminate\Support\Collection类。这意味着您可以使用Laravel集合中所有可用的方法。
searchBy()
searchBy帮助您通过其实体属性值找到Wikidata实体。
$results = $wikidata->searchBy($propId, $entityId, $lang, $limit);
参数
$propId: 要搜索的属性的id(必需)$entityId: 实体的id(必需)$lang: 指定结果语言(默认:'en')$limit: 设置自定义限制(默认:10)
示例
// 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: [] } ... ] } */
searchBy()方法始终返回包含结果的Illuminate\Support\Collection类。这意味着您可以使用Laravel集合中所有可用的方法。
get()
get()通过指定的ID返回Wikidata实体。
$entity = $wikidata->get($entityId, $lang);
参数
$entityId: 实体的id(必需)$lang: 指定结果语言(默认:'en')
示例
// 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" } ] } }, ... ] } }, ... ] */
升级指南
从3.1._升级到3.2._
新版本中对属性值存储方式的主要更改已经发生。现在每个属性可以有多个值。在这方面,Property对象中的value属性已被替换为values属性。
此外,值现在以Value对象的形式表示,这使我们能够保存每个值的字符串表示,以及其qualifiers列表。有关qualifiers的详细信息,请参阅此处。
测试
vendor/bin/phpunit
贡献
如果您发现错误或想为代码或文档做出贡献,您可以通过提交问题或拉取请求来提供帮助。
许可证
Wikidata遵循MIT许可证。
