Skip to content

Commit

Permalink
Merge pull request #33 from ipinfo/uman/map
Browse files Browse the repository at this point in the history
Add map integration.
  • Loading branch information
UmanShahzad authored Apr 22, 2021
2 parents 05798eb + 3543bd2 commit 0683234
Show file tree
Hide file tree
Showing 3 changed files with 1,037 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/IPinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,34 @@ public function getRequestDetails(string $ip_address)
return $raw_details;
}

/**
* Gets a URL to a map on https://ipinfo.io/map given a list of IPs (max
* 500,000).
* @param array $ips list of IP addresses to put on the map.
* @return string URL to the map.
*/
public function getMapUrl($ips)
{
$url = sprintf("%s/map?cli=1", self::API_URL);

try {
$response = $this->http_client->request(
'POST',
$url,
[
'json' => $ips
]
);
} catch (GuzzleException $e) {
throw new IPinfoException($e->getMessage());
} catch (Exception $e) {
throw new IPinfoException($e->getMessage());
}

$res = json_decode($response->getBody(), true);
return $res['reportUrl'];
}

/**
* Build headers for API request.
* @return array Headers for API request.
Expand Down Expand Up @@ -165,7 +193,7 @@ private function readCountryNames($countries_file)

/**
* Returns a versioned cache key given a user-input key.
* @param string key to transform into a versioned cache key.
* @param string $k key to transform into a versioned cache key.
* @return string the versioned cache key.
*/
private function cacheKey($k)
Expand Down
8 changes: 8 additions & 0 deletions tests/IPinfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,12 @@ public function testGuzzleOverride()
$this->expectException(IPinfoException::class);
$res = $h->getDetails($ip);
}

public function testGetMapURL()
{
$h = new IPinfo();
$url = $h->getMapUrl(file("tests/map-ips.txt"));
echo "got URL=".$url;
$this->assertStringStartsWith("https://ipinfo.io/map/", $url);
}
}
Loading

0 comments on commit 0683234

Please sign in to comment.