IPWho PHP SDK
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):
composer require ipwho/ipwho-ip-geolocation-apiOr 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): ?GeoLocationgetTimezone(?string $ip = null): ?TimezonegetConnection(?string $ip = null): ?ConnectiongetSecurity(?string $ip = null): ?SecuritygetMe(): IPWhoDatagetIp(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 controlleruse 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
Clientwith your API key or setX-API-Keyheader. - API errors: the client throws exceptions when the API returns an error or the payload is invalid.