v1.0.1
PriviMetrics 2
- <?php
-
- if (!function_exists('pm_fetch_location')) {
-
- function pm_fetch_location(string $ip): array
- {
- $service = env('GEO_EXTERNAL_SERVICE', 'ip-api');
- $apiKey = env('GEO_API_KEY', '');
-
- switch ($service) {
- case 'ip-api':
- return pm_geo_ip_api($ip);
- case 'ipinfo':
- return pm_geo_ipinfo($ip, $apiKey);
- case 'ipstack':
- return pm_geo_ipstack($ip, $apiKey);
- case 'ip2location':
- return pm_geo_ip2location($ip, $apiKey);
- case 'dbip':
- return pm_geo_dbip($ip);
- default:
- return ['country' => 'Unknown', 'code' => 'XX'];
- }
- }
-
- function pm_geo_ip_api(string $ip): array
- {
- $url = "http://ip-api.com/json/{$ip}?fields=status,country,countryCode";
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 2);
- $response = curl_exec($ch);
- if (PHP_VERSION_ID < 80000) {
- curl_close($ch);
- }
-
- $data = $response ? json_decode($response, true) : null;
- if ($data && ($data['status'] ?? '') === 'success') {
- return ['country' => $data['country'], 'code' => $data['countryCode']];
- }
- return ['country' => 'Unknown', 'code' => 'XX'];
- }
-
- function pm_geo_ipinfo(string $ip, string $token): array
- {
- if ($token === '') return ['country' => 'Unknown', 'code' => 'XX'];
- $url = "https://ipinfo.io/{$ip}/json?token=" . urlencode($token);
- $response = @file_get_contents($url);
- if ($response) {
- $data = json_decode($response, true);
- if ($data && isset($data['country'])) {
- return ['country' => $data['country'], 'code' => $data['country']];
- }
- }
- return ['country' => 'Unknown', 'code' => 'XX'];
- }
-
- function pm_geo_ipstack(string $ip, string $apiKey): array
- {
- if ($apiKey === '') return ['country' => 'Unknown', 'code' => 'XX'];
- $url = "http://api.ipstack.com/{$ip}?access_key=" . urlencode($apiKey);
- $response = @file_get_contents($url);
- if ($response) {
- $data = json_decode($response, true);
- if ($data && isset($data['country_name'], $data['country_code'])) {
- return ['country' => $data['country_name'], 'code' => $data['country_code']];
- }
- }
- return ['country' => 'Unknown', 'code' => 'XX'];
- }
-
- function pm_geo_ip2location(string $ip, string $apiKey): array
- {
- if ($apiKey === '') return ['country' => 'Unknown', 'code' => 'XX'];
- $url = "https://api.ip2location.io/?key=" . urlencode($apiKey) . "&ip={$ip}";
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);
- $response = curl_exec($ch);
- if (PHP_VERSION_ID < 80000) {
- curl_close($ch);
- }
-
- $data = $response ? json_decode($response, true) : null;
- if ($data && isset($data['country_code'])) {
- return ['country' => $data['country_name'], 'code' => $data['country_code']];
- }
- return ['country' => 'Unknown', 'code' => 'XX'];
- }
-
- function pm_geo_dbip(string $ip): array
- {
- $url = "https://api.db-ip.com/v2/free/{$ip}";
- $response = @file_get_contents($url);
- if ($response) {
- $data = json_decode($response, true);
- if ($data && isset($data['countryName'], $data['countryCode'])) {
- return ['country' => $data['countryName'], 'code' => $data['countryCode']];
- }
- }
- return ['country' => 'Unknown', 'code' => 'XX'];
- }
- }
-