Skip to content

IPWho PHP SDK

Packagist PHP 7.4+ MIT License

Official PHP client for the IPWho Geolocation API — fetch geolocation, timezone, connection and security information for IP addresses using a small PSR-4 compatible client.

Installation

Install via Composer (recommended):

Terminal window
composer require ipwho/ipwho-ip-geolocation-api

Or add to your composer.json:

{
"require": {
"ipwho/ipwho-ip-geolocation-api": "^1.0"
}
}

Quick Start

<?php
require 'vendor/autoload.php';
use SDKIpWho\\Client;
$client = new Client(getenv('IPWHO_API_KEY') ?: 'your-api-key');
$location = $client->getLocation();
print_r($location);

Example minimal response (PHP array/object representation):

// (simplified)
[
'country' => 'United States',
'countryCode' => 'US',
'city' => 'San Francisco',
'latitude' => 37.7749,
'longitude' => -122.4194
]

API Reference

Primary client methods (see src/Client.php):

  • getLocation(?string $ip = null): ?GeoLocation
  • getTimezone(?string $ip = null): ?Timezone
  • getConnection(?string $ip = null): ?Connection
  • getSecurity(?string $ip = null): ?Security
  • getMe(): IPWhoData
  • getIp(string $ip): IPWhoData

Requirements

Requires PHP ^7.4 || ^8.0. See composer.json for details.

Type Definitions

Methods return typed PHP objects/arrays as documented in the src/Types.php file.

Usage Examples

Basic usage (explicit IP):

$location = $client->getLocation('76.102.11.203');
print_r($location);

Handling errors:

try {
$data = $client->getIp('invalid-ip');
var_export($data);
} catch (\Exception $e) {
error_log('API or network error: ' . $e->getMessage());
}

Using in Laravel (example):

// in a controller
use SDKIpWho\\Client;
public function show(Client $client)
{
$loc = $client->getLocation();
return view('ip.show', ['location' => $loc]);
}

Notes:

  • Ensure composer autoload is present (require 'vendor/autoload.php').
  • Keep API keys out of version control; use environment variables or framework secrets.

Troubleshooting & Errors

  • Missing API key: instantiate Client with your API key or set X-API-Key header.
  • API errors: the client throws exceptions when the API returns an error or the payload is invalid.