Skip to content

Commit 0683234

Browse files
authored
Merge pull request #33 from ipinfo/uman/map
Add map integration.
2 parents 05798eb + 3543bd2 commit 0683234

File tree

3 files changed

+1037
-1
lines changed

3 files changed

+1037
-1
lines changed

src/IPinfo.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,34 @@ public function getRequestDetails(string $ip_address)
134134
return $raw_details;
135135
}
136136

137+
/**
138+
* Gets a URL to a map on https://ipinfo.io/map given a list of IPs (max
139+
* 500,000).
140+
* @param array $ips list of IP addresses to put on the map.
141+
* @return string URL to the map.
142+
*/
143+
public function getMapUrl($ips)
144+
{
145+
$url = sprintf("%s/map?cli=1", self::API_URL);
146+
147+
try {
148+
$response = $this->http_client->request(
149+
'POST',
150+
$url,
151+
[
152+
'json' => $ips
153+
]
154+
);
155+
} catch (GuzzleException $e) {
156+
throw new IPinfoException($e->getMessage());
157+
} catch (Exception $e) {
158+
throw new IPinfoException($e->getMessage());
159+
}
160+
161+
$res = json_decode($response->getBody(), true);
162+
return $res['reportUrl'];
163+
}
164+
137165
/**
138166
* Build headers for API request.
139167
* @return array Headers for API request.
@@ -165,7 +193,7 @@ private function readCountryNames($countries_file)
165193

166194
/**
167195
* Returns a versioned cache key given a user-input key.
168-
* @param string key to transform into a versioned cache key.
196+
* @param string $k key to transform into a versioned cache key.
169197
* @return string the versioned cache key.
170198
*/
171199
private function cacheKey($k)

tests/IPinfoTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,12 @@ public function testGuzzleOverride()
134134
$this->expectException(IPinfoException::class);
135135
$res = $h->getDetails($ip);
136136
}
137+
138+
public function testGetMapURL()
139+
{
140+
$h = new IPinfo();
141+
$url = $h->getMapUrl(file("tests/map-ips.txt"));
142+
echo "got URL=".$url;
143+
$this->assertStringStartsWith("https://ipinfo.io/map/", $url);
144+
}
137145
}

0 commit comments

Comments
 (0)