我有一个存放了多个ip的txt文件,写一个批量调用高德api查询ip地理位置,并返回省份和城市与ip一一对应的数据,数据保存到txt文件,查询数据量很大请设置间隔10秒查询一次。php代码
<?php
// 设置高德api的key
$key = "yourkey";
// 读取ip列表文件
$file = "ipList.txt";
$ips = file_get_contents($file);
$ips = explode("\n", $ips);
// 初始化结果数组
$results = array();
// 调用高德api查询每个ip的地理位置,并保存结果
foreach ($ips as $ip) {
$url = "https://restapi.amap.com/v3/ip?key=$key&ip=$ip";
$response = file_get_contents($url);
$json = json_decode($response, true);
// 解析结果
$province = $json["province"];
$city = $json["city"];
// 保存结果
$results[$ip] = array(
"province" => $province,
"city" => $city,
);
// 暂停10秒
sleep(10);
}
// 将结果保存到文件
$outputFile = "ipResults.txt";
$output = "";
foreach ($results as $ip => $data) {
$province = $data["province"];
$city = $data["city"];
$output .= "$ip,$province,$city\n";
}
file_put_contents($outputFile, $output);
?>
其中,你需要将代码中的 “yourkey” 替换为你自己的高德api key,并将输入文件和输出文件路径设置为你的文件所在路径。
代码将读取输入文件 ipList.txt
中的ip列表,并使用 file_get_contents
函数调用高德api查询每个ip的地理位置。为了避免调用频率过高,代码设置了每个请求之间的10秒暂停。查询结果将保存到一个数组中,然后将其写入输出文件 ipResults.txt
中。
最终,输出文件将包含每个IP的省份和城市信息。 在文件的每一行上,IP地址和其对应的省份与城市将使用逗号分隔。
发现免费高德api有时间和数量限制,而且有些ip高德不能返回定位有增加了一个阿里云api
先查询本地记录的,没有就去高德查询,高德查询不到,再去查询阿里云返回未知.
亲测可以直接用的
下面代码可以直接复制到php文件用即可
//1号优惠 · 51福利网www.51yhyh.com提供
$unknown_address_file = 'unknown_address.txt';
$success_address_file = 'success_address.txt';
$fail_address_file = 'fail_address.txt';
$ip = '';
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = trim($ips[count($ips) - 1]);
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
$address = null;
// 读取本地文件,获取地理位置
if (file_exists($success_address_file)) {
$addresses = json_decode(file_get_contents($success_address_file), true);
if (isset($addresses[$ip])) {
$address = $addresses[$ip];
}
}
// 如果之前没有获取过地址,尝试调用高德地图API
if ($address === null) {
$amap_key = '你的高德地图api';
$response = file_get_contents("https://restapi.amap.com/v3/ip?ip={$ip}&output=json&key={$amap_key}");
$data = json_decode($response, true);
if ($data['status'] == 1 && empty($data['province'])) {
$province = $data['province'];
$city = $data['city'];
$address = array(
'province' => $province,
'city' => $city
);
// 将地址保存到本地文件
$addresses = array();
if (file_exists($success_address_file)) {
$addresses = json_decode(file_get_contents($success_address_file), true);
}
$addresses[$ip] = $address;
file_put_contents($success_address_file, json_encode($addresses));
} else {
// 当高德地图API查询失败时,使用阿里云API查询1号优惠 · 51福利网www.51yhyh.com提供
$aliyun_appcode = '你的阿里云api';
$url = "https://api01.aliyun.venuscn.com/ip?ip={$ip}";
$headers = array(
'Authorization:APPCODE ' . $aliyun_appcode
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if ($data['code'] == 0 && !empty($data['data']['region'])) {
$province = $data['data']['region'];
$city = $data['data']['city'];
$address = array(
'province' => $province,
'city' => $city
);
// 将地址保存到本地文件
$addresses = array();
if (file_exists($success_address_file)) {
$addresses = json_decode(file_get_contents($success_address_file), true);
}
$addresses[$ip] = $address;
file_put_contents($success_address_file, json_encode($addresses));
} else {
// 将地址保存到本地文件
$addresses = array();
if (file_exists($fail_address_file)) {
$addresses = json_decode(file_get_contents($fail_address_file), true);
}
$addresses[$ip] = array();
file_put_contents($fail_address_file, json_encode($addresses));
}
}
}
if (empty($address)) {
// 将地址保存到本地文件
$addresses = array();
if (file_exists($unknown_address_file)) {
$addresses = json_decode(file_get_contents($unknown_address_file), true);
}
$addresses[$ip] = array();
file_put_contents($unknown_address_file, json_encode($addresses));
header('Location: 1.html');
exit;
}
$province = $address['province'];
$city = $address['city'];
$userdizhi = $province . ' ' . $city;
//echo "位置: " . $city;
$_SESSION['city'] = $city; //将变量$city存储到Session
$_SESSION['province'] = $province; //将变量$city存储到Session
$_SESSION['userdizhi'] = $userdizhi; //将变量$city存储到Session