Search by

snipershady / iplocationnet

snipershady

Typed, dependency-light PHP client for the IPLocation.net API v2 (IP geolocation, country lookup, public IP, MAC vendor lookup, proxy check, proxy list, SMTP validation)

Package info

github.com/snipershady/iplocationnet

pkg:composer/snipershady/iplocationnet

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-12-01 17:38 UTC

This package is auto-updated.

Last update: 2026-12-01 18:43:30 UTC


README

A lightweight, object-oriented PHP client for the IPLocation.net API v2. It covers every v2 endpoint (public IP, IP geolocation, IP country, MAC vendor lookup, proxy check, proxy list and SMTP validation) with typed, immutable responses, clear exceptions and a pluggable HTTP transport.

  • PHP >= 8.4, ext-curl, ext-json. No runtime dependency besides the PSR-18/PSR-17 interfaces.
  • Malformed IPs, MAC addresses and e-mails are rejected locally, so they never consume your daily allowance.
  • The API key is never hard-coded: your application passes it to the client.

Installation

composer require snipershady/iplocationnet

Quick start

use IpLocationNet\IpLocationNetClient;

$client = new IpLocationNetClient();
$client->setApiKey($apiKey); // or: new IpLocationNetClient($apiKey)

$location = $client->ipLocation('8.8.8.8');

echo $location->getCityName();    // Mountain View
echo $location->getCountryCode(); // US
echo $location->getAsn();         // 15169 (int)
echo $location->getLatitude();    // 37.4056 (float)

Get a free key at https://api.iplocation.net/api-key. Read it from your configuration (environment variable, secrets vault, framework parameter...) and pass it to the client.

Endpoints

Method Endpoint API key Daily allowance (free plan) Returns
getIp() /v2/get-ip no none PublicIp
ipCountry(string $ip) /v2/ip-country no none IpCountry
macLookup(string $mac) /v2/mac-lookup no none MacLookup
ipLocation(string $ip) /v2/ip-location yes 10 / day IpLocation
proxyCheck(string $ip) /v2/proxy-check yes 10 / day ProxyCheck
proxyList(?int $limit = null) /v2/proxy-list yes none ProxyList
smtpValidator(string $email) /v2/smtp-validator yes 10 / day SmtpValidation

The key, when set, is sent to every endpoint as Authorization: Bearer <key>. Calling an endpoint that needs a key without one throws MissingApiKeyException before any request is made.

Endpoint::requiresApiKey() and Endpoint::hasDailyQuota() expose the same information at runtime.

Examples

// Public IP of the caller
$client->getIp()->getIp();                              // "203.0.113.10"
$client->getIp()->getIpVersion();                       // IpVersion::V4

// Country (null fields for private/LAN ranges)
$country = $client->ipCountry('2001:4860:4860::8888');
$country->getCountryCode();                             // "US"

// MAC vendor: ":", "-", "." separators or none are accepted
$mac = $client->macLookup('00-00-00-12-34-56');
$mac->isFound();                                        // true
$mac->getVendor();                                      // "Xerox"

// Proxy / VPN / Tor detection
$check = $client->proxyCheck('139.59.51.139');
$check->isAnonymous();                                  // listed, proxy, VPN or Tor
$check->getDetails()->getFraudScore();                  // 68
$check->getProxy()?->getAddress();                      // "139.59.51.139:80"

// Public proxies
foreach ($client->proxyList(50)->filterByType('https')->filterByCountry('FR') as $proxy) {
    echo $proxy->getAddress(), ' ', $proxy->getUptime(), "%\n";
}

// SMTP validation
$smtp = $client->smtpValidator('john@example.com');
$smtp->isValid();
$smtp->getMxRecords();                                  // ["mail.example.com" => 10], lowest priority first

Every response object implements JsonSerializable, so json_encode($response) gives a normalized payload (typed values, null for missing data).

Error handling

IPLocation.net always answers HTTP 200 and reports failures in the response_code field of the body. The client turns them into exceptions. All of them implement IpLocationNet\Exception\IpLocationNetExceptionInterface.

Exception When
InvalidArgumentException Malformed IP/MAC/e-mail/limit/key/base URL, rejected locally (no request sent)
MissingApiKeyException Endpoint requires a key and none was set (no request sent)
AuthenticationException response_code 401/403, e.g. "Please enter valid key"
QuotaExceededException response_code 429 or a message mentioning a limit/quota
BadRequestException response_code 400, e.g. "Error: Invalid IP Address"
ApiErrorException Any other error code, e.g. 503
InvalidResponseException Body is not a well-formed API v2 JSON object
TransportException Network failure: DNS, connection, TLS, timeout

AuthenticationException, QuotaExceededException, BadRequestException and ApiErrorException extend ApiException, which exposes getEndpoint(), getResponseCode() and getResponseMessage().

use IpLocationNet\Exception\ApiException;
use IpLocationNet\Exception\IpLocationNetExceptionInterface;
use IpLocationNet\Exception\QuotaExceededException;

try {
    $location = $client->ipLocation($ip);
} catch (QuotaExceededException) {
    // fall back to ipCountry(), which has no daily allowance
} catch (ApiException $e) {
    $logger->warning($e->getMessage(), ['code' => $e->getResponseCode()]);
} catch (IpLocationNetExceptionInterface $e) {
    // anything else thrown by the library
}

HTTP transport

The default transport is CurlHttpClient (10 s connect timeout, 30 s total, HTTP/HTTPS only, TLS verified, no redirects):

use IpLocationNet\Http\CurlHttpClient;

$client = new IpLocationNetClient($apiKey, new CurlHttpClient(connectTimeoutMs: 3_000, timeoutMs: 10_000));

Any PSR-18 client works through Psr18HttpClient:

use IpLocationNet\Http\Psr18HttpClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\Psr18Client;

$factory = new Psr17Factory();
$client = new IpLocationNetClient($apiKey, new Psr18HttpClient(new Psr18Client(), $factory, $factory));

You can also implement IpLocationNet\Http\HttpClientInterface yourself, for example to add caching or logging.

Testing your code

Type-hint IpLocationNet\IpLocationNetClientInterface in your services and replace it with a test double. The response classes have public constructors, so you can build fixtures without touching the network.

Security

  • ApiKey hides the secret from var_dump()/print_r() (ipl_****kHM), and the key parameters carry #[\SensitiveParameter], so stack traces never contain it.
  • The key is validated against header injection (printable ASCII only, no whitespace).

Development

The QA toolchain is Rector, then php-cs-fixer, then PHPStan (level max with strict, deprecation and PHPUnit rules plus bleeding edge), then PHPUnit with a 100% coverage gate.

Run the tools with the PHP 8.4 binary if your default php is older:

php8.4 $(which composer) install
php8.4 $(which composer) qa          # validate, rector:check, cs:check, stan, test:coverage
php8.4 $(which composer) qa:fix      # rector + php-cs-fixer
Suite Command Network
unit composer test none: recording fake transport
integration composer test local PHP built-in web server on 127.0.0.1
live composer test:live real api.iplocation.net, consumes the daily allowance

The live suite reads IPLOCATIONNET_API_KEY from the environment or from .env.local (git-ignored and excluded from the dist package). Copy .env.local.dist to .env.local and set your key. Each run uses one request of each limited endpoint (ip-location, proxy-check, smtp-validator).

AI coding agents

AGENT_IPLOCATIONNET.md is an integration guide for coding agents. It ships in the dist package, so a consuming project can import it from its AGENTS.md:

@vendor/snipershady/iplocationnet/AGENT_IPLOCATIONNET.md

License

GPL-2.0-or-later. See LICENSE.