1919 */
2020class Ipdata
2121{
22- private const BASE_URL = 'https://api.ipdata.co ' ;
22+ private const DEFAULT_BASE_URL = 'https://api.ipdata.co ' ;
23+ public const EU_BASE_URL = 'https://eu-api.ipdata.co ' ;
2324
2425 /**
2526 * @var string|null
@@ -36,13 +37,18 @@ class Ipdata
3637 */
3738 private $ requestFactory ;
3839
40+ /**
41+ * @var string
42+ */
43+ private $ baseUrl ;
44+
3945 /**
4046 * Get an instance of the API client. Give it an API key, a PSR-18 client and a PSR-17 request factory.
4147 *
4248 * @param ClientInterface|null $httpClient if null, we will try to use php-http/discovery to find an installed client
4349 * @param RequestFactoryInterface|null $requestFactory if null, we will try to use php-http/discovery to find an installed factory
4450 */
45- public function __construct (string $ apiKey , ClientInterface $ httpClient = null , RequestFactoryInterface $ requestFactory = null )
51+ public function __construct (string $ apiKey , ClientInterface $ httpClient = null , RequestFactoryInterface $ requestFactory = null , string $ baseUrl = self :: DEFAULT_BASE_URL )
4652 {
4753 if (null === $ httpClient ) {
4854 if (!class_exists (Psr18ClientDiscovery::class)) {
@@ -71,14 +77,15 @@ public function __construct(string $apiKey, ClientInterface $httpClient = null,
7177 $ this ->httpClient = $ httpClient ;
7278 $ this ->apiKey = $ apiKey ;
7379 $ this ->requestFactory = $ requestFactory ;
80+ $ this ->baseUrl = $ baseUrl ;
7481 }
7582
7683 /**
7784 * @param array<string> $fields
7885 *
7986 * @throws \Psr\Http\Client\ClientExceptionInterface
8087 */
81- public function lookup (string $ ip , array $ fields = []): array
88+ public function lookup (string $ ip = '' , array $ fields = []): array
8289 {
8390 $ query = [
8491 'api-key ' => $ this ->apiKey ,
@@ -88,7 +95,8 @@ public function lookup(string $ip, array $fields = []): array
8895 $ query ['fields ' ] = implode (', ' , $ fields );
8996 }
9097
91- $ request = $ this ->requestFactory ->createRequest ('GET ' , sprintf ('%s/%s?%s ' , self ::BASE_URL , $ ip , http_build_query ($ query )));
98+ $ url = $ ip !== '' ? sprintf ('%s/%s ' , $ this ->baseUrl , $ ip ) : $ this ->baseUrl ;
99+ $ request = $ this ->requestFactory ->createRequest ('GET ' , sprintf ('%s?%s ' , $ url , http_build_query ($ query )));
92100 $ response = $ this ->httpClient ->sendRequest ($ request );
93101
94102 return $ this ->parseResponse ($ response );
@@ -97,11 +105,12 @@ public function lookup(string $ip, array $fields = []): array
97105 /**
98106 * Bulk lookup, requires paid subscription.
99107 *
108+ * @param array<string> $ips
100109 * @param array<string> $fields
101110 *
102111 * @throws \Psr\Http\Client\ClientExceptionInterface
103112 */
104- public function buildLookup (array $ ips , array $ fields = []): array
113+ public function bulkLookup (array $ ips , array $ fields = []): array
105114 {
106115 $ query = [
107116 'api-key ' => $ this ->apiKey ,
@@ -111,14 +120,27 @@ public function buildLookup(array $ips, array $fields = []): array
111120 $ query ['fields ' ] = implode (', ' , $ fields );
112121 }
113122
114- $ request = $ this ->requestFactory ->createRequest ('POST ' , sprintf ('%s/bulk?%s ' , self :: BASE_URL , http_build_query ($ query )));
123+ $ request = $ this ->requestFactory ->createRequest ('POST ' , sprintf ('%s/bulk?%s ' , $ this -> baseUrl , http_build_query ($ query )));
115124 $ request ->getBody ()->write (json_encode ($ ips ));
116- $ request = $ request ->withAddedHeader ('Content-Type ' , 'text/plain ' );
125+ $ request = $ request ->withHeader ('Content-Type ' , 'application/json ' );
117126 $ response = $ this ->httpClient ->sendRequest ($ request );
118127
119128 return $ this ->parseResponse ($ response );
120129 }
121130
131+ /**
132+ * @deprecated Use bulkLookup() instead.
133+ *
134+ * @param array<string> $ips
135+ * @param array<string> $fields
136+ *
137+ * @throws \Psr\Http\Client\ClientExceptionInterface
138+ */
139+ public function buildLookup (array $ ips , array $ fields = []): array
140+ {
141+ return $ this ->bulkLookup ($ ips , $ fields );
142+ }
143+
122144 private function parseResponse (ResponseInterface $ response ): array
123145 {
124146 $ body = $ response ->getBody ()->__toString ();
0 commit comments