dev-main 2025-06-15 12:47 UTC

This package is not auto-updated.

Last update: 2025-06-15 19:06:33 UTC


README

๐ŸŒ GeoIP PHP SDK

Developer-friendly & type-safe PHP SDK for enterprise IP geolocation services

MIT License PHP Version Composer Development Status

๐Ÿ“– Table of Contents

๐Ÿš€ Overview

The GeoIP PHP SDK is a high-performance, enterprise-grade library that provides real-time IP geolocation data for web applications. Built with modern PHP practices, this SDK offers type-safe interfaces and comprehensive error handling for seamless integration into your projects.

Perfect for applications requiring IP-based personalization, analytics, security monitoring, and geographic content delivery. The SDK supports multiple output formats including JSON, JSONP, XML, and YAML.

โœจ Key Features

  • ๐Ÿ”’ Type-safe - Full TypeScript-style type safety for PHP
  • โšก High Performance - Optimized for enterprise-scale applications
  • ๐ŸŒ Multiple Formats - JSON, JSONP, XML, and YAML support
  • ๐Ÿ›ก๏ธ Error Handling - Comprehensive exception management
  • ๐Ÿ“ฆ Composer Ready - Easy installation via Composer
  • ๐Ÿ”ง Configurable - Flexible server and endpoint configuration
  • ๐Ÿ“Š Real-time Data - Live IP geolocation information
  • ๐Ÿข Enterprise Grade - Built for production environments

๐Ÿ“ฆ Installation

Prerequisites

  • PHP 8.0 or higher
  • Composer 2.0+
  • cURL extension enabled

Install via Composer

Add the following to your composer.json file:

{
    "repositories": [
        {
            "type": "github",
            "url": "https://github.com/geoip/geo-ip-php.git"
        }
    ],
    "require": {
        "geoipapi/sdk": "*"
    }
}

Then run:

composer install

Alternatively, install directly:

composer require geoipapi/sdk

๐Ÿ”ง Quick Start

Get up and running in under 2 minutes:

<?php
declare(strict_types=1);

require 'vendor/autoload.php';

use geoipapi\geoipapi\GeoIp;

// Initialize the SDK
$sdk = GeoIp::builder()->build();

// Get current IP information
$response = $sdk->geoIPEndpoints->getIp();

if ($response->res !== null) {
    echo "Your IP: " . $response->res->ip . "\n";
    echo "Country: " . $response->res->country . "\n";
    echo "City: " . $response->res->city . "\n";
}

๐Ÿ“š Usage Examples

Get Current IP Address

$response = $sdk->geoIPEndpoints->getIp();

if ($response->res !== null) {
    $ipData = $response->res;
    echo "IP: {$ipData->ip}\n";
    echo "Country: {$ipData->country}\n";
    echo "Region: {$ipData->region}\n";
    echo "City: {$ipData->city}\n";
    echo "Timezone: {$ipData->timezone}\n";
}

Get Specific IP Information

try {
    $response = $sdk->geoIPEndpoints->getIpData([
        'ip' => '8.8.8.8',
        'format' => 'json'
    ]);
    
    if ($response->responseGetJsonDataJsonGet !== null) {
        $data = $response->responseGetJsonDataJsonGet;
        // Process geolocation data
    }
} catch (Errors\HTTPValidationErrorThrowable $e) {
    echo "Validation error: " . $e->getMessage();
} catch (Errors\APIException $e) {
    echo "API error: " . $e->getMessage();
}

Custom Server Configuration

$sdk = GeoIp::builder()
    ->setServerURL('https://api.geoipapi.com')
    ->build();

๐Ÿ—๏ธ Project Structure

geo-ip-php/
โ”œโ”€โ”€ src/                    # Source code
โ”‚   โ”œโ”€โ”€ Models/            # Data models and DTOs
โ”‚   โ”œโ”€โ”€ Endpoints/         # API endpoint classes
โ”‚   โ”œโ”€โ”€ Errors/           # Exception classes
โ”‚   โ””โ”€โ”€ GeoIp.php         # Main SDK class
โ”œโ”€โ”€ docs/                  # Documentation
โ”‚   โ””โ”€โ”€ sdks/             # SDK-specific docs
โ”œโ”€โ”€ tests/                # Test suite
โ”œโ”€โ”€ composer.json         # Composer configuration
โ”œโ”€โ”€ LICENSE              # MIT License
โ””โ”€โ”€ README.md           # This file

Key Components

  • src/GeoIp.php - Main SDK entry point and builder
  • src/Endpoints/ - Contains all API endpoint implementations
  • src/Models/ - Data transfer objects and response models
  • src/Errors/ - Custom exception classes for error handling
  • docs/ - Comprehensive API documentation

โš™๏ธ Configuration

Environment Variables

# Optional: Custom API endpoint
GEOIP_API_URL=https://api.geoipapi.com

# Optional: Request timeout (seconds)
GEOIP_TIMEOUT=30

# Optional: Enable debug mode
GEOIP_DEBUG=false

SDK Configuration Options

$sdk = GeoIp::builder()
    ->setServerURL('https://custom-api.example.com')
    ->setTimeout(60)
    ->setDebugMode(true)
    ->build();

๐Ÿงช Testing

Run the test suite to ensure everything works correctly:

# Run all tests
composer test

# Run tests with coverage
composer test:coverage

# Run specific test file
vendor/bin/phpunit tests/Unit/GeoIpTest.php

# Run integration tests
composer test:integration

Test Categories

  • Unit Tests - Test individual components in isolation
  • Integration Tests - Test API interactions and data flow
  • End-to-End Tests - Test complete user workflows

๐Ÿ”„ Available Resources and Operations

GeoIP Endpoints

Method Description Parameters
getIp() Get current IP address information None
getIpData(array $params) Get geolocation data for specific IP ip, format

Response Formats

  • JSON (default) - Structured data format
  • JSONP - JSON with padding for cross-domain requests
  • XML - Extensible markup language format
  • YAML - Human-readable data serialization

For detailed API documentation, see docs/sdks/geoipendpoints/README.md.

โŒ Error Handling

The SDK provides comprehensive error handling with specific exception types:

Exception Types

Exception Status Code Description
APIException 4XX, 5XX General API errors
HTTPValidationError 422 Request validation failures
NetworkException N/A Network connectivity issues

Error Handling Example

use geoipapi\geoipapi\Models\Errors;

try {
    $response = $sdk->geoIPEndpoints->getIpData(['ip' => 'invalid-ip']);
} catch (Errors\HTTPValidationErrorThrowable $e) {
    // Handle validation errors
    error_log("Validation failed: " . $e->getMessage());
} catch (Errors\APIException $e) {
    // Handle API errors
    error_log("API error: " . $e->getMessage());
    error_log("Status code: " . $e->statusCode);
} catch (Exception $e) {
    // Handle unexpected errors
    error_log("Unexpected error: " . $e->getMessage());
}

๐ŸŒ Server Configuration

Default Server

The SDK uses https://api.geoipapi.com as the default server endpoint.

Custom Server

Override the default server globally:

$sdk = GeoIp::builder()
    ->setServerURL('https://your-custom-api.com')
    ->build();

๐Ÿค Contributing

We welcome contributions! Here's how to get started:

Development Setup

  1. Fork and clone the repository
  2. Install dependencies: composer install
  3. Run tests: composer test
  4. Create a feature branch: git checkout -b feature/amazing-feature

Code Standards

  • Follow PSR-12 coding standards
  • Write comprehensive tests for new features
  • Update documentation for API changes
  • Use type declarations throughout

Pull Request Process

  1. Ensure all tests pass
  2. Update documentation if needed
  3. Add entries to CHANGELOG.md
  4. Submit PR with clear description

Development Commands

# Install dependencies
composer install

# Run code style checks
composer lint

# Fix code style issues
composer lint:fix

# Run static analysis
composer analyze

# Generate documentation
composer docs:generate

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Credits

Built With

  • PHP 8.0+ - Modern PHP features and performance
  • Composer - Dependency management
  • PSR Standards - PHP Standards Recommendations
  • PHPUnit - Testing framework

Acknowledgements

  • Thanks to all contributors who help improve this SDK
  • GeoIP data providers for accurate location information
  • The PHP community for excellent tooling and standards

Ready to get started? โญ Star this repo and try the SDK today!

Report Bug โ€ข Request Feature โ€ข Discussions