其实在提供这些服务的都比较多,但是大多都是收费的;且价格不菲,而离线数据库呢购买起来几乎都是上万;当然也有免费的;本文就给大家两个示列;如果自己有好的接口也可以进行更换;文章中所用的是免费的API接口,不保证其持续可用。如果失效请留言更新。
设备信息
Ps:这里包括访问者设备、IP地址、设备语言、IP归属地等信息
<?php
// 获取设备信息
function getDevice() {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
return $userAgent;
}
// 获取浏览器信息
function getBrowser() {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$browser = "Unknown";
if (strpos($userAgent, 'MSIE') !== false) {
$browser = 'Internet Explorer';
} elseif (strpos($userAgent, 'Firefox') !== false) {
$browser = 'Mozilla Firefox';
} elseif (strpos($userAgent, 'Chrome') !== false) {
$browser = 'Google Chrome';
} elseif (strpos($userAgent, 'Safari') !== false) {
$browser = 'Apple Safari';
} elseif (strpos($userAgent, 'Opera Mini') !== false) {
$browser = 'Opera Mini';
} elseif (strpos($userAgent, 'Opera') !== false) {
$browser = 'Opera';
} elseif (strpos($userAgent, 'Edge') !== false) {
$browser = 'Microsoft Edge';
} elseif (strpos($userAgent, 'Trident') !== false) {
$browser = 'Internet Explorer';
}
return $browser;
}
// 获取IP地址
function getIP() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
// 获取系统语言
function getLanguage() {
$language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
return $language;
}
// 获取IP位置
function getIPInfo($ip) {
$url = "http://ip-api.com/json/{$ip}";
$response = file_get_contents($url);
$data = json_decode($response, true);
return $data;
}
// 输出API结果
function outputAPI() {
$device = getDevice();
$browser = getBrowser();
$ip = getIP();
$language = getLanguage();
$ipInfo = getIPInfo($ip);
$apiResult = array(
'设备信息' => $browser,
'系统语言' => $language,
'浏览器信息' => $device,
'IP 地址' => $ip,
'IP 信息' => $ipInfo
);
header('Content-Type: application/json');
echo json_encode($apiResult);
}
// 调用API
outputAPI();
?>
IP归属地
<?php
function getIpLocation($ip) {
$url = "http://ip-api.com/json/{$ip}";
$response = file_get_contents($url);
$data = json_decode($response, true);
if ($data['status'] == 'success') {
$location = $data['country'] . ', ' . $data['regionName'] . ', ' . $data['city'];
return $location;
} else {
return 'Unknown';
}
}
$ip = $_SERVER['REMOTE_ADDR'];
$location = getIpLocation($ip);
$response = [
'ip' => $ip,
'location' => $location
];
header('Content-Type: application/json');
echo json_encode($response);