Geocoder\Provider\IpInfoDb::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 IpInfoDb provider does not support street addresses, only IPv4 addresses.');
        }
        // This API does not support IPv6
        if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            throw new UnsupportedOperation('The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses.');
        }
        if ('127.0.0.1' === $address) {
            return $this->returnResults([$this->getLocalhostDefaults()]);
        }
        $query = sprintf($this->endpointUrl, $this->apiKey, $address);
        return $this->executeQuery($query);
    }

Usage Example

Esempio n. 1
0
 /**
  * @group temp
  */
 public function testGetGeocodedDataWithCountryPrecision()
 {
     if (!isset($_SERVER['IPINFODB_API_KEY'])) {
         $this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml');
     }
     $provider = new IpInfoDb($this->getAdapter($_SERVER['IPINFODB_API_KEY']), $_SERVER['IPINFODB_API_KEY'], 'country');
     $results = $provider->geocode('74.125.45.100');
     $this->assertInstanceOf('Geocoder\\Model\\AddressCollection', $results);
     $this->assertCount(1, $results);
     /** @var \Geocoder\Model\Address $result */
     $result = $results->first();
     $this->assertInstanceOf('\\Geocoder\\Model\\Address', $result);
     $this->assertNull($result->getLatitude());
     $this->assertNull($result->getLongitude());
     $this->assertNull($result->getPostalCode());
     $this->assertNull($result->getLocality());
     $this->assertEmpty($result->getAdminLevels());
     $this->assertEquals('United States', $result->getCountry()->getName());
     $this->assertEquals('US', $result->getCountry()->getCode());
     $this->assertNull($result->getTimezone());
 }