Overview

Namespaces

  • GeoIp2
    • Database
    • Exception
    • Model
    • Record
    • WebService
  • PHP

Classes

  • Reader
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: 
  3: namespace GeoIp2\Database;
  4: 
  5: use GeoIp2\Exception\AddressNotFoundException;
  6: use GeoIp2\Model\City;
  7: use GeoIp2\Model\CityIspOrg;
  8: use GeoIp2\Model\Country;
  9: use GeoIp2\Model\Omni;
 10: use MaxMind\Db\Reader as DbReader;
 11: 
 12: /**
 13:  * Instances of this class provide a reader for the GeoIP2 database format.
 14:  * IP addresses can be looked up using the <code>country</code>
 15:  * and <code>city</code> methods. We also provide <code>cityIspOrg</code>
 16:  * and <code>omni</code> methods to ease compatibility with the web service
 17:  * client, although we may offer the ability to specify additional databases
 18:  * to replicate these web services in the future (e.g., the ISP/Org database).
 19:  *
 20:   * **Usage**
 21:  *
 22:  * The basic API for this class is the same for every database. First, you
 23:  * create a reader object, specifying a file name. You then call the method
 24:  * corresponding to the specific database, passing it the IP address you want
 25:  * to look up.
 26:  *
 27:  * If the request succeeds, the method call will return a model class for
 28:  * the method you called. This model in turn contains multiple record classes,
 29:  * each of which represents part of the data returned by the database. If
 30:  * the database does not contain the requested information, the attributes
 31:  * on the record class will have a <code>null</code> value.
 32:  *
 33:  * If the address is not in the database, an
 34:  * {@link \GeoIp2\Exception\AddressNotFoundException} exception will be
 35:  * thrown. If an invalid IP address is passed to one of the methods, a
 36:  * SPL {@link \InvalidArgumentException} will be thrown. If the database is
 37:  * corrupt or invalid, a {@link \MaxMind\Db\Reader\InvalidDatabaseException}
 38:  * will be thrown.
 39:  *
 40:  */
 41: class Reader
 42: {
 43:     private $dbReader;
 44:     private $languages;
 45: 
 46:     /**
 47:      * Constructor.
 48:      *
 49:      * @param string $filename The path to the GeoIP2 database file.
 50:      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
 51:      *          is corrupt or invalid
 52:      */
 53:     public function __construct(
 54:         $filename,
 55:         $languages = array('en')
 56:     ) {
 57:         $this->dbReader = new DbReader($filename);
 58:         $this->languages = $languages;
 59:     }
 60: 
 61:     /**
 62:      * This method returns a GeoIP2 City model.
 63:      *
 64:      * @param string $ipAddress IPv4 or IPv6 address as a string.
 65:      *
 66:      * @return \GeoIp2\Model\City
 67:      *
 68:      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
 69:      *         not in the database.
 70:      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
 71:      *         is corrupt or invalid
 72:      */
 73:     public function city($ipAddress)
 74:     {
 75:         return $this->modelFor('City', $ipAddress);
 76:     }
 77: 
 78:     /**
 79:      * This method returns a GeoIP2 Country model.
 80:      *
 81:      * @param string $ipAddress IPv4 or IPv6 address as a string.
 82:      *
 83:      * @return \GeoIp2\Model\Country
 84:      *
 85:      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
 86:      *         not in the database.
 87:      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
 88:      *         is corrupt or invalid
 89:      */
 90:     public function country($ipAddress)
 91:     {
 92:         return $this->modelFor('Country', $ipAddress);
 93:     }
 94: 
 95:     /**
 96:      * This method returns a GeoIP2 City/ISP/Org model.
 97:      *
 98:      * @param string $ipAddress IPv4 or IPv6 address as a string.
 99:      *
100:      * @return \GeoIp2\Model\CityIspOrg
101:      *
102:      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
103:      *         not in the database.
104:      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
105:      *         is corrupt or invalid
106:      */
107:     public function cityIspOrg($ipAddress)
108:     {
109:         return $this->modelFor('CityIspOrg', $ipAddress);
110:     }
111: 
112:     /**
113:      * This method returns a GeoIP2 Omni model.
114:      *
115:      * @param string $ipAddress IPv4 or IPv6 address as a string.
116:      *
117:      * @return \GeoIp2\Model\Omni
118:      *
119:      * @throws \GeoIp2\Exception\AddressNotFoundException if the address is
120:      *         not in the database.
121:      * @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
122:      *         is corrupt or invalid
123:      */
124:     public function omni($ipAddress)
125:     {
126:         return $this->modelFor('Omni', $ipAddress);
127:     }
128: 
129:     private function modelFor($class, $ipAddress)
130:     {
131:         $record = $this->dbReader->get($ipAddress);
132:         if ($record === null) {
133:             throw new AddressNotFoundException(
134:                 "The address $ipAddress is not in the database."
135:             );
136:         }
137:         $record['traits']['ip_address'] = $ipAddress;
138:         $class = "GeoIp2\\Model\\" . $class;
139: 
140:         return new $class($record, $this->languages);
141:     }
142: 
143:     /**
144:      * Closes the GeoIP2 database and returns the resources to the system.
145:      */
146:     public function close()
147:     {
148:         $this->dbReader->close();
149:     }
150: }
151: 
GeoIP2 PHP API v0.4.0 API documentation generated by ApiGen 2.8.0