PHP code example of iplocate / php-iplocate

1. Go to this page and download the library: Download iplocate/php-iplocate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iplocate / php-iplocate example snippets




use IPLocate\IPLocate;

// Create a client with your API key
// Get your free API key from https://iplocate.io/signup
$client = new IPLocate('your-api-key');

// Look up an IP address
$result = $client->lookup('8.8.8.8');

echo "IP: {$result->ip}\n";
if ($result->country) {
    echo "Country: {$result->country}\n";
}
if ($result->city) {
    echo "City: {$result->city}\n";
}

// Check privacy flags
echo "Is VPN: " . ($result->privacy->isVpn ? 'Yes' : 'No') . "\n";
echo "Is Proxy: " . ($result->privacy->isProxy ? 'Yes' : 'No') . "\n";

// Look up your own IP address (no IP parameter)
$result = $client->lookupSelf();
echo "Your IP: {$result->ip}\n";

$result = $client->lookup('203.0.113.1');
echo "Country: {$result->country} ({$result->countryCode})\n";

$result = $client->lookup('203.0.113.1');
echo "Currency: {$result->currencyCode}\n";

$result = $client->lookup('203.0.113.1');
echo "Calling code: +{$result->callingCode}\n";

$client = new IPLocate('your-api-key');

use IPLocate\IPLocate;

$client = new IPLocate('your-api-key');
$result = $client->lookup('203.0.113.1');

echo "Country: {$result->country} ({$result->countryCode})\n";
if ($result->latitude && $result->longitude) {
    echo "Coordinates: {$result->latitude}, {$result->longitude}\n";
}

$result = $client->lookup('192.0.2.1');

if ($result->privacy->isVpn) {
    echo "This IP is using a VPN\n";
}

if ($result->privacy->isProxy) {
    echo "This IP is using a proxy\n";
}

if ($result->privacy->isTor) {
    echo "This IP is using Tor\n";
}

$result = $client->lookup('8.8.8.8');

if ($result->asn) {
    echo "ASN: {$result->asn->asn}\n";
    echo "ISP: {$result->asn->name}\n";
    echo "Network: {$result->asn->route}\n";
}

use GuzzleHttp\Client;
use IPLocate\IPLocate;

// Custom timeout and HTTP client
$customHttpClient = new Client([
    'timeout' => 60.0,
    'verify' => true,
]);

$client = new IPLocate(
    apiKey: 'your-api-key',
    timeout: 60.0,
    httpClient: $customHttpClient
);

// Custom base URL (for enterprise customers)
$client = new IPLocate(
    apiKey: 'your-api-key',
    baseUrl: 'https://custom-endpoint.com/api'
);

class LookupResponse
{
    public string $ip;
    public Privacy $privacy;
    public ?string $country;
    public ?string $countryCode;
    public bool $isEu;
    public ?string $city;
    public ?string $continent;
    public ?float $latitude;
    public ?float $longitude;
    public ?string $timeZone;
    public ?string $postalCode;
    public ?string $subdivision;
    public ?string $currencyCode;
    public ?string $callingCode;
    public ?string $network;
    public ?ASN $asn;
    public ?Company $company;
    public ?Hosting $hosting;
    public ?Abuse $abuse;
}

use IPLocate\IPLocate;
use IPLocate\Exceptions\AuthenticationException;
use IPLocate\Exceptions\InvalidIPException;
use IPLocate\Exceptions\NotFoundException;
use IPLocate\Exceptions\RateLimitException;
use IPLocate\Exceptions\APIException;

$client = new IPLocate('your-api-key');

try {
    $result = $client->lookup('8.8.8.8');
} catch (InvalidIPException $e) {
    echo "Invalid IP address: {$e->getMessage()}\n";
} catch (AuthenticationException $e) {
    echo "Invalid API key: {$e->getMessage()}\n";
} catch (NotFoundException $e) {
    echo "IP address not found: {$e->getMessage()}\n";
} catch (RateLimitException $e) {
    echo "Rate limit exceeded: {$e->getMessage()}\n";
} catch (APIException $e) {
    echo "API error ({$e->getCode()}): {$e->getMessage()}\n";
}
bash
composer