Geocoder\Provider\GeoIPs::geocode PHP Method

geocode() public method

{@inheritDoc}
public geocode ( $address )
    public function geocode($address)
    {
        if (null === $this->apiKey) {
            throw new InvalidCredentials('No API key provided.');
        }
        if (!filter_var($address, FILTER_VALIDATE_IP)) {
            throw new UnsupportedOperation('The GeoIPs provider does not support street addresses, only IPv4 addresses.');
        }
        if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            throw new UnsupportedOperation('The GeoIPs provider does not support IPv6 addresses, only IPv4 addresses.');
        }
        if ('127.0.0.1' === $address) {
            return $this->returnResults([$this->getLocalhostDefaults()]);
        }
        $query = sprintf(self::GEOCODE_ENDPOINT_URL, $address, $this->apiKey);
        return $this->executeQuery($query);
    }

Usage Example

Example #1
0
 /**
  * @expectedException \Geocoder\Exception\NoResult
  */
 public function testGeocodeWithRealIPv4NoResults()
 {
     if (!isset($_SERVER['GEOIPS_API_KEY'])) {
         $this->markTestSkipped('You need to configure the GEOIPS_API_KEY value in phpunit.xml');
     }
     $provider = new GeoIPs($this->getAdapter($_SERVER['GEOIPS_API_KEY']), $_SERVER['GEOIPS_API_KEY']);
     $provider->geocode('255.255.150.96');
 }