Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
class ContactDetails
{
private $cityPhoneMap = [
'москва' => '8-800-123-12-12',
'лондон' => '8-800-123-12-13',
];
// Метод для получения города пользователя по его IP через ipwho.is
private function getCityFromIp(): string
{
$clientIp = "91.151.136.113"; // Получаем IP клиента
$ch = curl_init('http://ipwho.is/' . $clientIp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$apiResponse = curl_exec($ch);
curl_close($ch);
$locationData = json_decode($apiResponse, true);
return isset($locationData['city']) ? strtolower($locationData['city']) : 'москва';
}
private function getPhoneNumberByCity(): string
{
$userCity = $this->getCityFromIp();
return $this->cityPhoneMap[$userCity];
}
public function printContactInfo(): void
{
echo "Контактный телефон: {$this->getPhoneNumberByCity()}";
}
}
// Пример использования:
$contactPage = new ContactDetails();
$contactPage->printContactInfo();