|
| 1 | +# PHP GeoIP2 extension # |
| 2 | + |
| 3 | +## Description ## |
| 4 | + |
| 5 | +This package provides an API for the GeoIP2 |
| 6 | +[databases](http://dev.maxmind.com/geoip/geoip2/downloadable). The API also |
| 7 | +works with the free |
| 8 | +[GeoLite2 databases](http://dev.maxmind.com/geoip/geoip2/geolite2/). |
| 9 | + |
| 10 | +### Dependencies ### |
| 11 | + |
| 12 | +This extension depends on: |
| 13 | + |
| 14 | +``` |
| 15 | +php-maxminddb |
| 16 | +php-json |
| 17 | +``` |
| 18 | + |
| 19 | +## IP Geolocation Usage ## |
| 20 | + |
| 21 | +IP geolocation is inherently imprecise. Locations are often near the center of |
| 22 | +the population. Any location provided by a GeoIP2 database or web service |
| 23 | +should not be used to identify a particular address or household. |
| 24 | + |
| 25 | +## Database Reader ## |
| 26 | + |
| 27 | +### Usage ### |
| 28 | + |
| 29 | +To use this API, you must create a new `\GeoIP2\Database\Reader` object with |
| 30 | +the path to the database file as the first argument to the constructor. You |
| 31 | +may then call the method corresponding to the database you are using. |
| 32 | + |
| 33 | +If the lookup succeeds, the method call will return a model class for the |
| 34 | +record in the database. This model in turn contains multiple container |
| 35 | +classes for the different parts of the data such as the city in which the |
| 36 | +IP address is located. |
| 37 | + |
| 38 | +If the record is not found, a `\GeoIP2\Exception\AddressNotFoundException` |
| 39 | +is thrown. If the database is invalid or corrupt, a |
| 40 | +`\GeoIP2\Exception\InvalidDatabaseException` will be thrown. |
| 41 | + |
| 42 | +See the API documentation for more details. |
| 43 | + |
| 44 | +### City Example ### |
| 45 | + |
| 46 | +```php |
| 47 | +<?php |
| 48 | + |
| 49 | +use GeoIP2\Database\Reader; |
| 50 | + |
| 51 | +// This creates the Reader object, which should be reused across |
| 52 | +// lookups. |
| 53 | +$reader = new Reader('/usr/share/GeoIP/GeoIP2-City.mmdb'); |
| 54 | + |
| 55 | +// Replace "city" with the appropriate method for your database, e.g., |
| 56 | +// "country". |
| 57 | +$record = $reader->city('128.101.101.101'); |
| 58 | + |
| 59 | +print($record->country->isoCode . "\n"); // 'US' |
| 60 | +print($record->country->name . "\n"); // 'United States' |
| 61 | +print($record->country->names['zh-CN'] . "\n"); // '美国' |
| 62 | + |
| 63 | +print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota' |
| 64 | +print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN' |
| 65 | + |
| 66 | +print($record->city->name . "\n"); // 'Minneapolis' |
| 67 | + |
| 68 | +print($record->postal->code . "\n"); // '55455' |
| 69 | + |
| 70 | +print($record->location->latitude . "\n"); // 44.9733 |
| 71 | +print($record->location->longitude . "\n"); // -93.2323 |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +### Anonymous IP Example ### |
| 76 | + |
| 77 | +```php |
| 78 | +<?php |
| 79 | + |
| 80 | +use GeoIP2\Database\Reader; |
| 81 | + |
| 82 | +// This creates the Reader object, which should be reused across |
| 83 | +// lookups. |
| 84 | +$reader = new Reader('/usr/share/GeoIP/GeoIP2-Anonymous-IP.mmdb'); |
| 85 | + |
| 86 | +$record = $reader->anonymousIp('128.101.101.101'); |
| 87 | + |
| 88 | +if ($record->isAnonymous) { print "anon\n"; } |
| 89 | +print($record->ipAddress . "\n"); // '128.101.101.101' |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +### Connection-Type Example ### |
| 94 | + |
| 95 | +```php |
| 96 | +<?php |
| 97 | + |
| 98 | +use GeoIP2\Database\Reader; |
| 99 | + |
| 100 | +// This creates the Reader object, which should be reused across |
| 101 | +// lookups. |
| 102 | +$reader = new Reader('/usr/share/GeoIP/GeoIP2-Connection-Type.mmdb'); |
| 103 | + |
| 104 | +$record = $reader->connectionType('128.101.101.101'); |
| 105 | + |
| 106 | +print($record->connectionType . "\n"); // 'Corporate' |
| 107 | +print($record->ipAddress . "\n"); // '128.101.101.101' |
| 108 | + |
| 109 | +``` |
| 110 | + |
| 111 | +### Domain Example ### |
| 112 | + |
| 113 | +```php |
| 114 | +<?php |
| 115 | + |
| 116 | +use GeoIP2\Database\Reader; |
| 117 | + |
| 118 | +// This creates the Reader object, which should be reused across |
| 119 | +// lookups. |
| 120 | +$reader = new Reader('/usr/share/GeoIP/GeoIP2-Domain.mmdb'); |
| 121 | + |
| 122 | +$record = $reader->domain('128.101.101.101'); |
| 123 | + |
| 124 | +print($record->domain . "\n"); // 'umn.edu' |
| 125 | +print($record->ipAddress . "\n"); // '128.101.101.101' |
| 126 | + |
| 127 | +``` |
| 128 | + |
| 129 | +### Enterprise Example ### |
| 130 | + |
| 131 | +```php |
| 132 | +<?php |
| 133 | + |
| 134 | +use GeoIP2\Database\Reader; |
| 135 | + |
| 136 | +// This creates the Reader object, which should be reused across |
| 137 | +// lookups. |
| 138 | +$reader = new Reader('/usr/share/GeoIP/GeoIP2-Enterprise.mmdb'); |
| 139 | + |
| 140 | +// Use the ->enterprise method to do a lookup in the Enterprise database |
| 141 | +$record = $reader->enterprise('128.101.101.101'); |
| 142 | + |
| 143 | +print($record->country->confidence . "\n"); // 99 |
| 144 | +print($record->country->isoCode . "\n"); // 'US' |
| 145 | +print($record->country->name . "\n"); // 'United States' |
| 146 | +print($record->country->names['zh-CN'] . "\n"); // '美国' |
| 147 | + |
| 148 | +print($record->mostSpecificSubdivision->confidence . "\n"); // 77 |
| 149 | +print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota' |
| 150 | +print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN' |
| 151 | + |
| 152 | +print($record->city->confidence . "\n"); // 60 |
| 153 | +print($record->city->name . "\n"); // 'Minneapolis' |
| 154 | + |
| 155 | +print($record->postal->code . "\n"); // '55455' |
| 156 | + |
| 157 | +print($record->location->accuracyRadius . "\n"); // 50 |
| 158 | +print($record->location->latitude . "\n"); // 44.9733 |
| 159 | +print($record->location->longitude . "\n"); // -93.2323 |
| 160 | + |
| 161 | +``` |
| 162 | + |
| 163 | +### ISP Example ### |
| 164 | + |
| 165 | +```php |
| 166 | +<?php |
| 167 | + |
| 168 | +use GeoIP2\Database\Reader; |
| 169 | + |
| 170 | +// This creates the Reader object, which should be reused across |
| 171 | +// lookups. |
| 172 | +$reader = new Reader('/usr/share/GeoIP/GeoIP2-ISP.mmdb'); |
| 173 | + |
| 174 | +$record = $reader->isp('128.101.101.101'); |
| 175 | + |
| 176 | +print($record->autonomousSystemNumber . "\n"); // 217 |
| 177 | +print($record->autonomousSystemOrganization . "\n"); // 'University of Minnesota' |
| 178 | +print($record->isp . "\n"); // 'University of Minnesota' |
| 179 | +print($record->organization . "\n"); // 'University of Minnesota' |
| 180 | + |
| 181 | +print($record->ipAddress . "\n"); // '128.101.101.101' |
| 182 | + |
| 183 | +``` |
| 184 | + |
| 185 | +## Values to use for Database or Array Keys ## |
| 186 | + |
| 187 | +**We strongly discourage you from using a value from any `names` property as |
| 188 | +a key in a database or array.** |
| 189 | + |
| 190 | +These names may change between releases. Instead we recommend using one of the |
| 191 | +following: |
| 192 | + |
| 193 | +* `GeoIP2\Record\City` - `$city->geonameId` |
| 194 | +* `GeoIP2\Record\Continent` - `$continent->code` or `$continent->geonameId` |
| 195 | +* `GeoIP2\Record\Country` and `GeoIP2\Record\RepresentedCountry` - |
| 196 | + `$country->isoCode` or `$country->geonameId` |
| 197 | +* `GeoIP2\Record\Subdivision` - `$subdivision->isoCode` or `$subdivision->geonameId` |
| 198 | + |
| 199 | +### What data is returned? ### |
| 200 | + |
| 201 | +While many of the end points return the same basic records, the attributes |
| 202 | +which can be populated vary between end points. In addition, while an end |
| 203 | +point may offer a particular piece of data, MaxMind does not always have every |
| 204 | +piece of data for any given IP address. |
| 205 | + |
| 206 | +Because of these factors, it is possible for any end point to return a record |
| 207 | +where some or all of the attributes are unpopulated. |
| 208 | + |
| 209 | +See the |
| 210 | +[GeoIP2 Precision web service docs](http://dev.maxmind.com/geoip/geoip2/web-services) |
| 211 | +for details on what data each end point may return. |
| 212 | + |
| 213 | +The only piece of data which is always returned is the `ipAddress` |
| 214 | +attribute in the `GeoIP2\Record\Traits` record. |
| 215 | + |
| 216 | +## Integration with GeoNames ## |
| 217 | + |
| 218 | +[GeoNames](http://www.geonames.org/) offers web services and downloadable |
| 219 | +databases with data on geographical features around the world, including |
| 220 | +populated places. They offer both free and paid premium data. Each |
| 221 | +feature is unique identified by a `geonameId`, which is an integer. |
| 222 | + |
| 223 | +Many of the records returned by the GeoIP2 web services and databases |
| 224 | +include a `geonameId` property. This is the ID of a geographical feature |
| 225 | +(city, region, country, etc.) in the GeoNames database. |
| 226 | + |
| 227 | +Some of the data that MaxMind provides is also sourced from GeoNames. We |
| 228 | +source things like place names, ISO codes, and other similar data from |
| 229 | +the GeoNames premium data set. |
| 230 | + |
| 231 | +## Reporting data problems ## |
| 232 | + |
| 233 | +If the problem you find is that an IP address is incorrectly mapped, |
| 234 | +please |
| 235 | +[submit your correction to MaxMind](http://www.maxmind.com/en/correction). |
| 236 | + |
| 237 | +If you find some other sort of mistake, like an incorrect spelling, |
| 238 | +please check the [GeoNames site](http://www.geonames.org/) first. Once |
| 239 | +you've searched for a place and found it on the GeoNames map view, there |
| 240 | +are a number of links you can use to correct data ("move", "edit", |
| 241 | +"alternate names", etc.). Once the correction is part of the GeoNames |
| 242 | +data set, it will be automatically incorporated into future MaxMind |
| 243 | +releases. |
| 244 | + |
| 245 | +If you are a paying MaxMind customer and you're not sure where to submit |
| 246 | +a correction, please |
| 247 | +[contact MaxMind support](http://www.maxmind.com/en/support) for help. |
| 248 | + |
| 249 | +## Other Support ## |
| 250 | + |
| 251 | +Please report all issues with this code using the |
| 252 | +[GitHub issue tracker](https://github.com/php-extensions/php-geoip2/issues). |
| 253 | + |
| 254 | +If you are having an issue with a MaxMind service that is not specific |
| 255 | +to the client API, please see |
| 256 | +[MaxMind support page](http://www.maxmind.com/en/support). |
| 257 | + |
| 258 | +## Requirements ## |
| 259 | + |
| 260 | +This library requires PHP 5.4 or greater. |
| 261 | + |
| 262 | +This library also relies on the [MaxMind DB Reader](https://github.com/maxmind/MaxMind-DB-Reader-php). |
| 263 | + |
| 264 | +## Contributing ## |
| 265 | + |
| 266 | +Patches and pull requests are encouraged. All code should follow the PSR-2 |
| 267 | +style guidelines. Please include unit tests whenever possible. You may obtain |
| 268 | +the test data for the maxmind-db folder by running `git submodule update |
| 269 | +--init --recursive` or adding `--recursive` to your initial clone, or from |
| 270 | +https://github.com/maxmind/MaxMind-DB |
| 271 | + |
0 commit comments