项目作者: php-extensions

项目描述 :
MaxMind GeoIP2 PHP extension
高级语言: C
项目地址: git://github.com/php-extensions/php-geoip2.git
创建时间: 2017-08-26T01:26:31Z
项目社区:https://github.com/php-extensions/php-geoip2

开源协议:

下载


PHP GeoIP2 extension

Description

This extension provides an API for the MaxMind
GeoIP2 databases and free
GeoLite2 databases.

This extension is a port of the official MaxMind GeoIP2 PHP package
and MaxMind DB Reader PHP package.
It is written in Zephir and converted to C.

Requirements

Since version 0.9.2 this extension requires PHP 7 or greater.

Building extension

Prerequisites

Since version 0.9.2, gcc 7+ is required. To enable it on CentOS:

  1. yum install -y yum-utils centos-release-scl
  2. yum -y --enablerepo=centos-sclo-rh-testing install devtoolset-7-gcc
  3. echo "source /opt/rh/devtoolset-7/enable" | sudo tee -a /etc/profile
  4. source /opt/rh/devtoolset-7/enable

Building with Zephir (recommended)

  1. zephir compile
  2. zephir install

Building without Zephir

  1. cd ext
  2. phpize
  3. ./configure
  4. make
  5. make install

This extension can also read custom databases created using
MaxMind DB Writer.

  1. use GeoIP2\Database\Reader;
  2. $ipAddress = '8.8.8.8';
  3. $databaseFile = 'mydb.mmdb';
  4. $reader = new Reader($databaseFile);
  5. print_r($record = $reader->custom($ipAddress));
  6. print_r($record->raw['color']);
  7. print_r($record->get('dogs'));

IP Geolocation Usage

IP geolocation is inherently imprecise. Locations are often near the center of
the population. Any location provided by a GeoIP2 database or web service
should not be used to identify a particular address or household.

Database Reader

Usage

To use this API, you must create a new \GeoIP2\Database\Reader object with
the path to the database file as the first argument to the constructor. You
may then call the method corresponding to the database you are using.

If the lookup succeeds, the method call will return a model class for the
record in the database. This model in turn contains multiple container
classes for the different parts of the data such as the city in which the
IP address is located.

If the record is not found, a \GeoIP2\Exception\AddressNotFoundException
is thrown. If the database is invalid or corrupt, a
\GeoIP2\Exception\InvalidDatabaseException will be thrown.

See the API documentation for more details.

City Example

  1. <?php
  2. use GeoIP2\Database\Reader;
  3. // This creates the Reader object, which should be reused across
  4. // lookups.
  5. $reader = new Reader('/usr/share/GeoIP/GeoIP2-City.mmdb');
  6. // Replace "city" with the appropriate method for your database, e.g.,
  7. // "country".
  8. $record = $reader->city('128.101.101.101');
  9. print($record->country->isoCode . "\n"); // 'US'
  10. print($record->country->name . "\n"); // 'United States'
  11. print($record->country->names['zh-CN'] . "\n"); // '美国'
  12. print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
  13. print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
  14. print($record->city->name . "\n"); // 'Minneapolis'
  15. print($record->postal->code . "\n"); // '55455'
  16. print($record->location->latitude . "\n"); // 44.9733
  17. print($record->location->longitude . "\n"); // -93.2323

Anonymous IP Example

  1. <?php
  2. use GeoIP2\Database\Reader;
  3. // This creates the Reader object, which should be reused across
  4. // lookups.
  5. $reader = new Reader('/usr/share/GeoIP/GeoIP2-Anonymous-IP.mmdb');
  6. $record = $reader->anonymousIp('128.101.101.101');
  7. if ($record->isAnonymous) { print "anon\n"; }
  8. print($record->ipAddress . "\n"); // '128.101.101.101'

Connection-Type Example

  1. <?php
  2. use GeoIP2\Database\Reader;
  3. // This creates the Reader object, which should be reused across
  4. // lookups.
  5. $reader = new Reader('/usr/share/GeoIP/GeoIP2-Connection-Type.mmdb');
  6. $record = $reader->connectionType('128.101.101.101');
  7. print($record->connectionType . "\n"); // 'Corporate'
  8. print($record->ipAddress . "\n"); // '128.101.101.101'

Domain Example

  1. <?php
  2. use GeoIP2\Database\Reader;
  3. // This creates the Reader object, which should be reused across
  4. // lookups.
  5. $reader = new Reader('/usr/share/GeoIP/GeoIP2-Domain.mmdb');
  6. $record = $reader->domain('128.101.101.101');
  7. print($record->domain . "\n"); // 'umn.edu'
  8. print($record->ipAddress . "\n"); // '128.101.101.101'

Enterprise Example

  1. <?php
  2. use GeoIP2\Database\Reader;
  3. // This creates the Reader object, which should be reused across
  4. // lookups.
  5. $reader = new Reader('/usr/share/GeoIP/GeoIP2-Enterprise.mmdb');
  6. // Use the ->enterprise method to do a lookup in the Enterprise database
  7. $record = $reader->enterprise('128.101.101.101');
  8. print($record->country->confidence . "\n"); // 99
  9. print($record->country->isoCode . "\n"); // 'US'
  10. print($record->country->name . "\n"); // 'United States'
  11. print($record->country->names['zh-CN'] . "\n"); // '美国'
  12. print($record->mostSpecificSubdivision->confidence . "\n"); // 77
  13. print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
  14. print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
  15. print($record->city->confidence . "\n"); // 60
  16. print($record->city->name . "\n"); // 'Minneapolis'
  17. print($record->postal->code . "\n"); // '55455'
  18. print($record->location->accuracyRadius . "\n"); // 50
  19. print($record->location->latitude . "\n"); // 44.9733
  20. print($record->location->longitude . "\n"); // -93.2323

ISP Example

  1. <?php
  2. use GeoIP2\Database\Reader;
  3. // This creates the Reader object, which should be reused across
  4. // lookups.
  5. $reader = new Reader('/usr/share/GeoIP/GeoIP2-ISP.mmdb');
  6. $record = $reader->isp('128.101.101.101');
  7. print($record->autonomousSystemNumber . "\n"); // 217
  8. print($record->autonomousSystemOrganization . "\n"); // 'University of Minnesota'
  9. print($record->isp . "\n"); // 'University of Minnesota'
  10. print($record->organization . "\n"); // 'University of Minnesota'
  11. print($record->ipAddress . "\n"); // '128.101.101.101'

Values to use for Database or Array Keys

We strongly discourage you from using a value from any names property as
a key in a database or array.

These names may change between releases. Instead we recommend using one of the
following:

  • GeoIP2\Record\City - $city->geonameId
  • GeoIP2\Record\Continent - $continent->code or $continent->geonameId
  • GeoIP2\Record\Country and GeoIP2\Record\RepresentedCountry -
    $country->isoCode or $country->geonameId
  • GeoIP2\Record\Subdivision - $subdivision->isoCode or $subdivision->geonameId

What data is returned?

While many of the end points return the same basic records, the attributes
which can be populated vary between end points. In addition, while an end
point may offer a particular piece of data, MaxMind does not always have every
piece of data for any given IP address.

Because of these factors, it is possible for any end point to return a record
where some or all of the attributes are unpopulated.

See the
GeoIP2 Precision web service docs
for details on what data each end point may return.

The only piece of data which is always returned is the ipAddress
attribute in the GeoIP2\Record\Traits record.

Integration with GeoNames

GeoNames offers web services and downloadable
databases with data on geographical features around the world, including
populated places. They offer both free and paid premium data. Each
feature is unique identified by a geonameId, which is an integer.

Many of the records returned by the GeoIP2 web services and databases
include a geonameId property. This is the ID of a geographical feature
(city, region, country, etc.) in the GeoNames database.

Some of the data that MaxMind provides is also sourced from GeoNames. We
source things like place names, ISO codes, and other similar data from
the GeoNames premium data set.

Reporting data problems

If the problem you find is that an IP address is incorrectly mapped,
please
submit your correction to MaxMind.

If you find some other sort of mistake, like an incorrect spelling,
please check the GeoNames site first. Once
you’ve searched for a place and found it on the GeoNames map view, there
are a number of links you can use to correct data (“move”, “edit”,
“alternate names”, etc.). Once the correction is part of the GeoNames
data set, it will be automatically incorporated into future MaxMind
releases.

If you are a paying MaxMind customer and you’re not sure where to submit
a correction, please
contact MaxMind support for help.

Other Support

Please report all issues with this code using the
GitHub issue tracker.

If you are having an issue with a MaxMind service that is not specific
to the client API, please see
MaxMind support page.