Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

inline data #69

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 102 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ $client = new IPinfo($access_token);
$ip_address = '216.239.36.21';
$details = $client->getDetails($ip_address);

$details->city; // Emeryville
$details->loc; // 37.8342,-122.2900
echo $details->city; // Mountain View
echo $details->loc; // 37.4056,-122.0775
```

### Usage
Expand All @@ -48,8 +48,8 @@ The `IPinfo->getDetails()` method accepts an IP address as an optional, position
$client = new IPinfo();
$ip_address = '216.239.36.21';
$details = $client->getDetails($ip_address);
$details->city; // Emeryville
$details->loc; // 37.8342,-122.2900
echo $details->city; // Mountain View
echo $details->loc; // 37.4056,-122.0775
```

### Authentication
Expand Down Expand Up @@ -129,9 +129,9 @@ $details->continent['name']; // North America
`Details->latitude` and `Details->longitude` will return latitude and longitude, respectively, as strings. `Details->loc` will still return a composite string of both values.

```php
$details->loc; // 34.0293,-118.3570
$details->latitude; // 34.0293
$details->longitude; // -118.3570
$details->loc; // 37.4056,-122.0775
$details->latitude; // 37.4056
$details->longitude; // -122.0775
```

#### Accessing all properties
Expand All @@ -141,27 +141,93 @@ $details->longitude; // -118.3570
```php
$details->all;
/*
{
'asn': { 'asn': 'AS20001',
'domain': 'twcable.com',
'name': 'Time Warner Cable Internet LLC',
'route': '104.172.0.0/14',
'type': 'isp'},
'city': 'Los Angeles',
'company': { 'domain': 'twcable.com',
'name': 'Time Warner Cable Internet LLC',
'type': 'isp'},
'country': 'US',
'country_name': 'United States',
'hostname': 'cpe-104-175-221-247.socal.res.rr.com',
'ip': '104.175.221.247',
'loc': '34.0293,-118.3570',
'latitude': '34.0293',
'longitude': '-118.3570',
'phone': '323',
'postal': '90016',
'region': 'California'
}
(
[ip] => 216.239.36.21
[hostname] => any-in-2415.1e100.net
[anycast] => 1
[city] => Mountain View
[region] => California
[country] => US
[loc] => 37.4056,-122.0775
[org] => AS15169 Google LLC
[postal] => 94043
[timezone] => America/Los_Angeles
[asn] => Array
(
[asn] => AS15169
[name] => Google LLC
[domain] => google.com
[route] => 216.239.36.0/24
[type] => hosting
)

[company] => Array
(
[name] => Google LLC
[domain] => google.com
[type] => hosting
)

[privacy] => Array
(
[vpn] =>
[proxy] =>
[tor] =>
[relay] =>
[hosting] => 1
[service] =>
)

[abuse] => Array
(
[address] => US, CA, Mountain View, 1600 Amphitheatre Parkway, 94043
[country] => US
[email] => network-abuse@google.com
[name] => Abuse
[network] => 216.239.32.0/19
[phone] => +1-650-253-0000
)

[domains] => Array
(
[ip] => 216.239.36.21
[total] => 2535948
[domains] => Array
(
[0] => pub.dev
[1] => virustotal.com
[2] => blooket.com
[3] => go.dev
[4] => rytr.me
)

)

[country_name] => United States
[is_eu] =>
[country_flag] => Array
(
[emoji] => 🇺🇸
[unicode] => U+1F1FA U+1F1F8
)

[country_flag_url] => https://cdn.ipinfo.io/static/images/countries-flags/US.svg
[country_currency] => Array
(
[code] => USD
[symbol] => $
)

[continent] => Array
(
[code] => NA
[name] => North America
)

[latitude] => 37.4056
[longitude] => -122.0775
)

*/
```

Expand Down Expand Up @@ -234,16 +300,16 @@ Please see [the official documentation](https://ipinfo.io/developers/batch) for

When looking up an IP address, the response object includes a `Details->country_name` attribute which includes the country name based on American English. It is possible to return the country name in other languages by setting the `countries_file` keyword argument when creating the `IPinfo` object.

The file must be a `.json` file with the following structure:
The file must be a `php` file with the following structure:

```JSON
{
"BD": "Bangladesh",
"BE": "Belgium",
"BF": "Burkina Faso",
"BG": "Bulgaria"
```php
[
"BD" => "Bangladesh",
"BE" => "Belgium",
"BF" => "Burkina Faso",
"BG" => "Bulgaria"
...
}
]
```

### Other Libraries
Expand Down
38 changes: 12 additions & 26 deletions src/IPinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ipinfo\ipinfo;

require_once __DIR__ . '/const.php';

use Exception;
use ipinfo\ipinfo\cache\DefaultCache;
use GuzzleHttp\Pool;
Expand All @@ -25,11 +27,11 @@ class IPinfo
const CACHE_TTL = 86400; // 24 hours as seconds
const CACHE_KEY_VSN = '1'; // update when cache vals change for same key.

const COUNTRIES_FILE_DEFAULT = __DIR__ . '/countries.json';
const COUNTRIES_FLAGS_FILE_DEFAULT = __DIR__ . '/flags.json';
const EU_COUNTRIES_FILE_DEFAULT = __DIR__ . '/eu.json';
const COUNTRIES_CURRENCIES_FILE_DEFAULT = __DIR__ . '/currency.json';
const CONTINENT_FILE_DEFAULT = __DIR__ . '/continent.json';
const COUNTRIES_FILE_DEFAULT = COUNTRIES;
const EU_COUNTRIES_FILE_DEFAULT = EU;
const COUNTRIES_FLAGS_FILE_DEFAULT = FLAGS;
const COUNTRIES_CURRENCIES_FILE_DEFAULT = CURRENCY;
const CONTINENT_FILE_DEFAULT = CONTINENTS;

const BATCH_MAX_SIZE = 1000;
const BATCH_TIMEOUT = 5; // seconds
Expand Down Expand Up @@ -63,16 +65,11 @@ public function __construct($access_token = null, $settings = [])
}
$this->http_client = new Client($guzzle_opts);

$countries_file = $settings['countries_file'] ?? self::COUNTRIES_FILE_DEFAULT;
$countries_flags_file = $settings['countries_flags_file'] ?? self::COUNTRIES_FLAGS_FILE_DEFAULT;
$countries_currencies_file = $settings['countries_currencies_file'] ?? self::COUNTRIES_CURRENCIES_FILE_DEFAULT;
$eu_countries_file = $settings['eu_countries_file'] ?? self::EU_COUNTRIES_FILE_DEFAULT;
$continents_file = $settings['continent_file'] ?? self::CONTINENT_FILE_DEFAULT;
$this->countries = $this->readJSONFile($countries_file);
$this->countries_flags = $this->readJSONFile($countries_flags_file);
$this->countries_currencies = $this->readJSONFile($countries_currencies_file);
$this->eu_countries = $this->readJSONFile($eu_countries_file);
$this-> continents = $this->readJSONFile($continents_file);
$this->countries = $settings['countries_file'] ?? self::COUNTRIES_FILE_DEFAULT;
$this->countries_flags = $settings['countries_flags_file'] ?? self::COUNTRIES_FLAGS_FILE_DEFAULT;
$this->countries_currencies = $settings['countries_currencies_file'] ?? self::COUNTRIES_CURRENCIES_FILE_DEFAULT;
$this->eu_countries = $settings['eu_countries_file'] ?? self::EU_COUNTRIES_FILE_DEFAULT;
$this-> continents = $settings['continent_file'] ?? self::CONTINENT_FILE_DEFAULT;

if (!array_key_exists('cache_disabled', $this->settings) || $this->settings['cache_disabled'] == false) {
if (array_key_exists('cache', $settings)) {
Expand Down Expand Up @@ -304,17 +301,6 @@ private function buildHeaders()
return $headers;
}

/**
* Read JSON from a file and return as an array.
* @param string $countries_file JSON file of country_code => country_name mappings
* @return array country_code => country_name mappings
*/
private function readJSONFile($countries_file)
{
$file_contents = file_get_contents($countries_file);
return json_decode($file_contents, true);
}

/**
* Returns a versioned cache key given a user-input key.
* @param string $k key to transform into a versioned cache key.
Expand Down
Loading