-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
Rumen Damyanov edited this page Jul 31, 2025
·
1 revision
Get up and running with php-geolocation in under 5 minutes! This guide will walk you through installation and your first geolocation detection.
composer require rumenx/php-geolocation<?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}";// 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]
// ]// 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'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// In a controller
public function index(Request $request)
{
$geo = new Geolocation();
$country = $geo->getCountryCode();
return view('welcome', compact('country'));
}// In a controller
public function index(): Response
{
$geo = new Geolocation();
$country = $geo->getCountryCode();
return $this->render('home.html.twig', [
'country' => $country
]);
}- 📖 Basic Usage Examples - More detailed examples and patterns
- ⚡ Laravel Integration - Complete Laravel setup with middleware
- 🎭 Local Development Simulation - Advanced testing capabilities
- 🌍 Language Negotiation - Multi-language website support
- 📚 Check the troubleshooting guide
- 🐛 Report issues on GitHub
- 💬 See examples directory for working code
Next: Installation & Setup for detailed configuration options.