Skip to content

Quick Start

Rumen Damyanov edited this page Jul 31, 2025 · 1 revision

Quick Start Guide

Get up and running with php-geolocation in under 5 minutes! This guide will walk you through installation and your first geolocation detection.

Installation

composer require rumenx/php-geolocation

Basic Usage

1. Simple Country Detection

<?php
require_once 'vendor/autoload.php';

use Rumenx\Geolocation\Geolocation;

// Initialize with current request
$geo = new Geolocation();

// Get visitor's country
$country = $geo->getCountryCode(); // e.g., 'US', 'CA', 'DE'

echo "Visitor is from: {$country}";

2. Get All Information

// Get comprehensive visitor information
$info = $geo->getGeoInfo();

print_r($info);
// Output:
// [
//     'country_code' => 'US',
//     'ip' => '192.168.1.1',
//     'preferred_language' => 'en-US',
//     'all_languages' => ['en-US', 'en'],
//     'browser' => ['browser' => 'Chrome', 'version' => '91.0'],
//     'os' => 'Windows 10',
//     'device' => 'desktop',
//     'resolution' => ['width' => 1920, 'height' => 1080]
// ]

3. Language Detection

// Country to language mapping
$countryToLanguage = [
    'US' => ['en'],
    'CA' => ['en', 'fr'],
    'DE' => ['de'],
    'FR' => ['fr'],
];

$geo = new Geolocation($_SERVER, $countryToLanguage);

// Get detected language
$language = $geo->getLanguageForCountry($geo->getCountryCode());
echo "Suggested language: {$language}"; // e.g., 'en', 'fr', 'de'

Local Development

The package automatically detects local development environments and provides simulation:

// This works in local development without Cloudflare
$geo = new Geolocation();

if ($geo->isLocalDevelopment()) {
    // Simulate a specific country for testing
    $geo = Geolocation::simulate('DE'); // German visitor
}

$country = $geo->getCountryCode(); // Returns 'DE' in simulation

Framework Integration Examples

Laravel (Quick Example)

// In a controller
public function index(Request $request)
{
    $geo = new Geolocation();
    $country = $geo->getCountryCode();

    return view('welcome', compact('country'));
}

Symfony (Quick Example)

// In a controller
public function index(): Response
{
    $geo = new Geolocation();
    $country = $geo->getCountryCode();

    return $this->render('home.html.twig', [
        'country' => $country
    ]);
}

What's Next?

Need Help?


Next: Installation & Setup for detailed configuration options.

Clone this wiki locally