|
本帖最后由 QAQ 于 2019-5-2 17:31 编辑
- <?php
- $gb2312 = $_GET["gb2312"] ?? 0 ? 1 : 0;
- function get_taobao_ip($ip,$gb2312 = 0){
- $dn = "taobao_ipcache";
- if(!file_exists($dn)){
- mkdir($dn);
- }
- $iplong = ip2long($ip);
- if(long2ip($iplong) != $ip){
- return;
- }
- $h16 = $iplong >> 16;$l16 = $iplong &65535;$fn = "{$dn}\\{$h16}.txt";
- $url = "http://ip.taobao.com/service/getIpInfo.php?ip={$ip}";
- $data = unserialize(@file_get_contents($fn));
- if($data == null) $data = array();
- if(isset($data[$l16])){
- $return = $data[$l16];
- } else {
- $return = @file_get_contents($url);
- if(json_decode($return,1) == array()){
- return;
- }
- $return = iconv("UTF-8","GB2312",$return);
- $data[$l16] = $return;
- @file_put_contents($fn,serialize($data));
- }
- return $gb2312 ? $return : iconv("GB2312","UTF-8",$return);
- }
- $out = get_taobao_ip($_GET["ip"] ?? "",$gb2312);
- if($out == null){
- header("HTTP/1.1 502 Bad Gateway");
- } else {
- header("Content-type:text/html;charset=".($gb2312 ? "gb2312" : "utf-8"));
- echo $out;
- }
- exit;
- ?>
复制代码 |
|