-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathApiClient.php
More file actions
84 lines (69 loc) · 2.49 KB
/
ApiClient.php
File metadata and controls
84 lines (69 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace Wikidata;
use Illuminate\Support\Collection;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class ApiClient
{
public const API_ENDPOINT = 'https://www.wikidata.org/w/api.php';
public function __construct(private ?HttpClientInterface $client=null)
{
if (!$this->client) {
$this->client = HttpClient::create();
}
}
/**
* Get all entities by their ids from wikidata api
*
* @param array|string $ids The IDs of the entities to get the data from (eg.: Q2, Q2|Q3)
* @param string $lang Language (default: en)
* @param array|string $props Array of the properties to get back from each entity (supported: aliases, claims, datatype, descriptions, info, labels, sitelinks, sitelinks/urls)
*
* @return \Illuminate\Support\Collection
*/
public function getEntities(array|string $ids, string $lang = 'en', array|string $props = []): Collection
{
$ids = is_array($ids) ? implode('|', $ids) : $ids;
$props = $props ? implode('|', $props) : null;
$response = $this->client->request('GET', self::API_ENDPOINT, [
'query' => [
'action' => 'wbgetentities',
'format' => 'json',
'languages' => $lang,
'ids' => $ids,
'sitefilter' => $lang . 'wiki',
'props' => $props,
],
]);
$results = json_decode($response->getContent(), true);
$data = $results['entities'] ?? [];
return collect($data);
}
/**
* Searches for entities using labels and aliases
*
* @param string $query
* @param string $lang Language (default: en)
* @param int $limit Max count of returning items (default: 10)
*
* @return \Illuminate\Support\Collection
*/
public function searchEntities($query, $lang = 'en', int $limit = 10)
{
$response = $this->client->request('GET', self::API_ENDPOINT, [
'query' => [
'action' => 'wbsearchentities',
'format' => 'json',
'strictlanguage' => true,
'language' => $lang,
'uselang' => $lang,
'search' => $query,
'limit' => $limit,
'props' => '',
],
]);
$results = json_decode($response->getContent(), true);
$data = $results['search'] ?? [];
return collect($data);
}
}