设为首页收藏本站淘宝杂货铺

从F到0 - From F to 0

 找回密码
 注册已关闭
搜索
查看: 1758|回复: 1
收起左侧

PHP7间接代理访问淘宝查询IP归属地返回JSON文本(带GB2312/UTF-8编码输出+TXT数据缓存)

[复制链接]
发表于 2019-5-2 17:20:33 | 显示全部楼层 |阅读模式
本帖最后由 QAQ 于 2019-5-2 17:31 编辑
  1. <?php
  2. $gb2312 = $_GET["gb2312"] ?? 0 ? 1 : 0;
  3. function get_taobao_ip($ip,$gb2312 = 0){
  4.         $dn = "taobao_ipcache";
  5.         if(!file_exists($dn)){
  6.                 mkdir($dn);
  7.         }
  8.         $iplong = ip2long($ip);
  9.         if(long2ip($iplong) != $ip){
  10.                 return;
  11.         }
  12.         $h16 = $iplong >> 16;$l16 = $iplong &65535;$fn = "{$dn}\\{$h16}.txt";
  13.         $url = "http://ip.taobao.com/service/getIpInfo.php?ip={$ip}";
  14.         $data = unserialize(@file_get_contents($fn));
  15.         if($data == null) $data = array();
  16.                         if(isset($data[$l16])){
  17.                         $return =  $data[$l16];
  18.                 } else {
  19.                         $return = @file_get_contents($url);
  20.                                 if(json_decode($return,1) == array()){
  21.                                         return;
  22.                                 }
  23.                         $return = iconv("UTF-8","GB2312",$return);
  24.                         $data[$l16] = $return;
  25.                         @file_put_contents($fn,serialize($data));
  26.                 }

  27.         return $gb2312 ? $return : iconv("GB2312","UTF-8",$return);
  28. }

  29. $out = get_taobao_ip($_GET["ip"] ?? "",$gb2312);
  30.         if($out == null){
  31.         header("HTTP/1.1 502 Bad Gateway");
  32.         } else {
  33.         header("Content-type:text/html;charset=".($gb2312 ? "gb2312" : "utf-8"));
  34.         echo $out;
  35.         }
  36. exit;
  37. ?>
复制代码
发表于 2019-5-2 23:17:26 来自手机 | 显示全部楼层
6666 支持一下
您需要登录后才可以回帖 登录 | 注册已关闭

本版积分规则

QQ|手机版|Archiver|从F到0 ( 蒙ICP备17002595号-1 )
蒙公网安备15010402000325号

腾讯云安全认证

GMT+8, 2024-3-28 15:58 , Processed in 0.559032 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表