首页 > 编程技术 > php

淘宝IP地址库API接口 (PHP)通过ip获取地址信息

发布时间:2016-11-25 15:43

我们这里介绍利用淘宝IP地址库API接口 (PHP)通过ip获取地址信息的方法,前面介绍过新浪,有道的这里介绍淘宝的吧。

淘宝IP地址库网址:http://ip.taobao.com/

淘宝地址库API接口

提供的服务包括:
1. 根据用户提供的IP地址,快速查询出该IP地址所在的地理信息和地理相关的信息,包括国家、省、市和运营商。
2. 用户可以根据自己所在的位置和使用的IP地址更新我们的服务内容。

接口说明:
1. 请求接口(GET方式):
http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]

2. 响应信息(json格式数据):
国家 、省(自治区或直辖市)、市(县)、运营商

3. 返回数据格式:

 代码如下 复制代码
{"code":0,"data":{"ip":"210.75.225.254","country":"u4e2du56fd","area":"u534eu5317",
"region":"u5317u4eacu5e02","city":"u5317u4eacu5e02","county":"","isp":"u7535u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}

其中code的值的含义为,0:成功,1:失败。
4. PHP代码示例:

 代码如下 复制代码

function getCity($ip)
{
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$ip=json_decode(file_get_contents($url));
if((string)$ip->code=='1'){
  return false;
  }
  $data = (array)$ip->data;
return $data;
}
$ip='221.216.64.183';
print_r(getCity($ip));exit;

5.获取IP地址php代码

<?php
error_reporting (E_ERROR | E_WARNING | E_PARSE);
if($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]){
$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
}
elseif($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]){
$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
}
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"]){
$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
}
elseif (getenv("HTTP_X_FORWARDED_FOR")){
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
elseif (getenv("HTTP_CLIENT_IP")){
$ip = getenv("HTTP_CLIENT_IP");
}
elseif (getenv("REMOTE_ADDR")){
$ip = getenv("REMOTE_ADDR");
}
else{
$ip = "Unknown";
}
echo $ip;
?>

搜索文件很简单只要用户输入目录我们就会自动遍历目录找到相关联的文件名并列出来,下面我们一起来看看吧。

php文件查找程序,输入一个路径确定后会遍历目录下所有的文件和文件夹,通过递归可以找到文件夹下面的每一个文件,再通过文件名和输入的关键字匹配,则可以查找到你想要的文件。对于本地,我们可以利用windows自带的查找去进行查找,但是对于线上的话,如查找ftp空间里面文件,本程序是很有用的。

php文件查找器


php文件查找器源码:

 代码如下 复制代码

<html>

 <head>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <title>php版文件查找(file search)</title>

 </head>

 <body>

  <form action="" method="post">

  <p> 文件查找(注:区分大小写)</p>

  <p>路径:<input type="text" name="path" /></p>

  <p>查找:<input type="text" name="key" /></p>

  <p><input type="submit" name="sub" value=" 开 始 " /></p>

  </form>

 </body>

</html>

<?php

/*

 * 注:区分大小写

 */

if(!empty($_POST['path'])&&!empty($_POST['key'])){

 echo "在路径 ".$_POST['path']."/ 中查找 ".$_POST['key']." 的结果为:<hr/>";

 $file_num = $dir_num = 0;

 $r_file_num = $r_dir_num= 0;

 $findFile = $_POST['key'];

 function delDirAndFile( $dirName ){ 

  if ( $handle = @opendir( "$dirName" ) ) {

   while ( false !== ( $item = readdir( $handle ) ) ) { 

    if ( $item != "." && $item != ".." ) { 

     if ( is_dir( "$dirName/$item" ) ) { 

      delDirAndFile( "$dirName/$item" );

     } else { 

      $GLOBALS['file_num']++;

      if(strstr($item,$GLOBALS['findFile'])){

       echo " <span><b> $dirName/$item </b></span><br />n";

       $GLOBALS['r_file_num']++;

      }

     } 

    }

   }

   closedir( $handle ); 

   $GLOBALS['dir_num']++;

   if(strstr($dirName,$GLOBALS['findFile'])){

    $loop = explode($GLOBALS['findFile'],$dirName);

    $countArr = count($loop)-1;

    if(empty($loop[$countArr])){

     echo " <span style='color:#297C79;'><b> $dirName </b></span><br />n";

     $GLOBALS['r_dir_num']++;

    }

   }

  }else{

   die("没有此路径!");

  }

 }

 


 delDirAndFile($_POST['path']);

 echo "<hr/>本次共搜索到".$file_num."个文件,文件夹".$dir_num."个<br/>";

 echo "<hr/>符合结果的共".$r_file_num."个文件,文件夹".$r_dir_num."个<br/>";

}

 


?>

上面只是查找文件,下面看一个查找文件中的字符是否包括我们要找的东西

自己写的一个批量查找文件内容的php程序,我是拿来扫描文件特征码的,现在我 贴出代码,供大家参考

 代码如下 复制代码


<?php

if ($_POST ['Submit'] == '开始') {

 $total = 0; //文件总数

 $dangerous = array (); //危险文件

 $dangerous_content = $_POST ["sstr"];

 $find_path = $_POST ["searchpath"];

 $shortname = $_POST ["shortname"];

 echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";

 echo "<html>";

 echo "<head>";

 echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";

 echo "</head>";

 echo "<body>";

 $begin_time=date("U");

 // $dangerous_content = "小亮,Root_GP,Root_CSS,c99sh_updateurl,c99sh_sourcesurl,640684770";

 visitFile ( $find_path, $shortname );

 $end_time=date("U");

 foreach ($dangerous as $d){

  echo $d."<br/>";

 }

 echo "查找文件总数:" . $total." 危险文件:".count($dangerous)." 总用时".($end_time-$begin_time)."秒";

 echo "</body>";

 echo "</html>";

 //if (! empty ( $dangerous )) {

  //foreach ( $dangerous as $dan ) {

 //echo "[error]" . $dan . "<br/>";

 //}

 //}

 exit();

}

function visitFile($path, $ext) {

 global $total;

 global $dangerous_content;

 $fdir = dir ( $path );

 //echo "Handle: " . $d->handle . "<br>";

 // echo "Path: " . $fdir->path . "<br>";

 set_time_limit ( 24 * 60 * 60 );

 

 while ( ($entry = $fdir->read ()) !== false ) {

  $pathSub = $path . "\" . $entry;

  if ($entry != '.' && $entry != '..') {

   if (is_dir ( $pathSub )) {

    visitFile ( $pathSub, $ext );

   } else {

    $exten = explode ( '.', $entry );

    $exten = array_reverse ( $exten ); //把上面数组倒序

    //   foreach ()

    $shortnames = explode ( '|', $ext );

    foreach ( $shortnames as $sn ) {

     if (! empty ( $exten ) && $sn == $exten [0]) {

      $total = $total + 1;

      //echo "开始分析文件:".$path."/".$entry . "<br>";

      $content = file_get_contents ( $path . "/" . $entry ); //这个性能较好

      $content = strtolower ( $content ); //全部转为小写

      $dangerous_content = strtolower ( $dangerous_content ); //全部转为小写

      isExists ( $dangerous_content, $path . "/" . $entry, $content );//这个方法太耗内存了,希望有高手能解决一下

     }

    }

    //sleep(1);

   }

  }

 }

 $fdir->close ();

}

function isExists($str, $filename, $content) {

 global $dangerous;

 //sleep ( 1 );

 set_time_limit ( 10 );

 $arr = explode ( ',', $str );

 $signature="特征码:";

 if (! empty ( $arr )) {

  //  $content = file_get_contents ( $filename ); //这个性能较好

  $content = strtolower ( $content ); //全部转为小写

  $error_count = 0;

  foreach ( $arr as $a ) {

   if (trim ( $a ) != "") {

    if (strpos ( $content, $a )) {

     $error_count = $error_count + 1;

     $signature.=$a." ";

    }

   }

  }

  if ($error_count > 0) {

//   $dangerous [] = $filename;

   $dangerous [] = "[error] " . $error_count . " " .$signature." " . $filename;

   //echo "[error] " . $error_count . " " .$signature." " . $filename . "<br/>";

  }else{

   //echo "[ok] "  . $filename . "<br/>";

  }

 }

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>批量查询文件</title>

<style type="text/css">

body {

 background: #FFFFFF;

 color: #000;

 font-size: 12px;

}

 

#top {

 text-align: center;

}

 

h1,p,form {

 margin: 0;

 padding: 0;

}

 

h1 {font-size; 14px;

 

}

</style>

</head>

<body>

<div id="top">

<h1>批量查找程序</h1>

<div>本程序可以扫描指定目录的所有文件,进行<strong>内容查找</strong>。<br />

在文件数量非常多的情况下,本操作比较占用服务器资源,请确脚本超时限制时间允许更改,否则可能无法完成操作。</div>

</div>

 

 

<form action="<?=$_SERVER ['SCRIPT_NAME']?>" name="form1"

 target="stafrm" method="post">

<table width="95%" border="0" align="center" cellpadding="3"

 cellspacing="1" bgcolor="#666666">

 <tr>

  <td width="10%" bgcolor="#FFFFFF"><strong>&nbsp;起始根路径:</strong></td>

  <td width="90%" bgcolor="#FFFFFF"><input name="searchpath" type="text"

   id="searchpath" value="D:/" size="20" /> 点表示当前目录,末尾不要加/ </td>

 </tr>

 <tr>

  <td bgcolor="#FFFFFF"><strong>&nbsp;文件扩展名:</strong></td>

  <td bgcolor="#FFFFFF"><input name="shortname" type="text"

   id="shortname" size="20" value="htm|html|shtml|php" /> 多个请用|隔开</td>

 </tr>

 <tr id="rpct">

  <td height="64" colspan="2" bgcolor="#FFFFFF">

  <table width="100%" border="0" cellspacing="1" cellpadding="1">

   <tr bgcolor="#EDFCE2">

    <td colspan="4"><strong>内容查找选项:</strong> <input type="checkbox"

     name="isreg" value="1" />使用正则表达式</td>

   </tr>

   <tr>

    <td colspan="4">查找内容类默认使用字符串查找,也可以使用正则表达式(需勾选)。"查找为"不填写的话,就表示删除"查找内容"。

     <br />com,system,exec,eval,escapeshell,cmd,passthru,base64_decode,gzuncompress

    </td>

   </tr>

   <tr>

    <td width="10%">&nbsp;查找内容:</td>

    <td width="36%" colspan="3"><textarea name="sstr" id="sstr"

     style="width: 90%; height: 45px">小亮,Root_GP,Root_CSS,c99sh_updateurl,c99sh_sourcesurl,640684770,hx_dealdir,while(1)</textarea></td>

   </tr>

  </table>

  </td>

 </tr>

 <tr>

  <td colspan="2" height="20" align="center" bgcolor="#E2F5BC"><input

   type="submit" name="Submit" value="开始" class="inputbut" /></td>

 </tr>

</table>

</form>

<table width="95%" border="0" align="center" cellpadding="3"

 cellspacing="1" bgcolor="#666666">

 <tr bgcolor="#FFFFFF">

  <td id="mtd">

  <div id='mdv' style='width: 100%; height: 100;'><iframe name="stafrm"

   frameborder="0" id="stafrm" width="100%" height="100%"></iframe></div>

  <script type="text/javascript">

    document.all.mdv.style.pixelHeight = screen.height - 450;

    </script></td>

 </tr>

</table>

</body>

</html>

文章介绍利用相关api接口实现根据客户端IP地址获得所属城市地区一个php实例,下面我们一起来看看。

今天分享一个PHP根据客户端IP地址获得所属地的小程序,以前写的,已经用了很久了,感觉这个接口还是停稳定的。虽然现在网上也有很多类似的,但是我觉得我这个还是很不错的,核心代码只用3行就能实现。虽然功能不是很强大,但是我们平时算是够用的。以下是代码:

 

 代码如下 复制代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>通过IP获取归属地</title>

</head>

 

 

 


图1 : 返回数据

 

 


图2 : 查询成功

 


如果喜欢的话,可以试试,几行代码就能搞定,我觉得还是挺实用的!!

本文章来源于 代潇瑞博客 原文地址:http://www.daixiaorui.com/read/3.html

<body>

<form action="" method="post">

 <p>请输入ip地址:<input type="text" name="ip" /></p>

 <p><input type="submit" value="查询"  /></p>

</form>

<?php

 //获取ip地址

 //$ip = $_SERVER['REMOTE_ADDR'];    //自动获取客户端的IP

 //ip对应的地区

 if(!empty($_POST['ip'])){

  $ip = $_POST['ip'];

  //接口地址,这样返回的是一个xml结果集,如图1;

  $str = file_get_contents("http://www.yodao.com/smartresult-xml/search.s?type=ip&q=".$ip);

  //这里要得到里面的地址信息,提取xml方法有很多,我用的是正则。

  preg_match_all( "/<location>(.*?)</location>/",$str,$addr1);

  //最终结果,如图2;

  $addr = $ip."=>".$addr1[1][0];

  echo $addr;

 }

?>

</body>

</html>


图1 : 返回数据


图2 : 查询成功


如果喜欢的话,可以试试,几行代码就能搞定,我觉得还是挺实用的!!

剩余时间是指一篇文章什么时候发布到现在有几分钟或几天了,这个在很多博客论坛都以看到文章1天以前发布的,下面我介绍两实例

把两个日期格式的字符串转化成unix时间戳,然后相减获得时间戳差。最后判断剩余时间,生成类似(2小时30分钟20秒前发布)这样的时间格式

 代码如下 复制代码

public function gettime($time_s,$time_n){
$time_s = strtotime($time_s);
$time_n = strtotime($time_n);
$strtime = '';
$time = $time_n-$time_s;
if($time >= 86400){
return $strtime = date('Y-m-d H:i:s',$time_s);
}
if($time >= 3600){
$strtime .= intval($time/3600).'小时';
$time = $time % 3600;
}else{
$strtime .= '';
}
if($time >= 60){
$strtime .= intval($time/60).'分钟';
$time = $time % 60;
}else{
$strtime .= '';
}
if($time > 0){
$strtime .= intval($time).'秒前';
}else{
$strtime = "时间错误";
}
return $strtime;
}

首先判断想减之后的值是否大于一天的秒数86400秒,如果大于的话就返回原来的数据库查询出来的时间

然后判断是否在1小时到一天之内,也就是3600秒-86400秒,如果是之内的话就返回X小时。得到结果后还需使用取余数的方法去除小时部分的时间,使用 %取余

然后判断是否在1分钟到一小时之内,也就是60秒-3600秒,如果是之内的话就返回X分钟。得到结果后还需使用取余数的方法去除分钟部分的时间,使用 %取余

最后判断是否在1分钟之内,也就是0秒-60秒,如果是之内的话就返回X分秒

注意:上面得出的结果都是使用.=连接的。这样最后才得到一个整体的时间。

页面缓存就是把页面保存到一个文件中,下次读出时直接调用文件而不查询数据库,这里我们介绍利用ob_start()来实现。


 代码如下 复制代码

<?php
ob_start(); //打开缓冲区
phpinfo(); //使用phpinfo函数
$info=ob_get_contents(); //得到缓冲区的内容并且赋值给$info
$file=fopen(’info.txt’,’w’); //打开文件info.txt
fwrite($file,$info); //写入信息到info.txt
fclose($file); //关闭文件info.txt
//或直接用 file_put_content('info.txt',$info);
?>

以上的方法,可以把不同用户的phpinfo信息保存下来。

这里我们可以着重看看这个方法的使用技巧,用这个方法可以实现生成静态页面的便利!

并且用这个方法比用file_get_conents()的方法更合理更有效率。

简单的说个应用吧,比如想要把phpinfo()的内容写入文件,可以这样做:

 代码如下 复制代码

ob_start();
$phpinfo = phpinfo();
//写入文件
ob_end_flush();

或者还有这样的用途:

ob_start(); //打开缓冲区
echo "Hellon"; //输出
header("location:index.php"); //把浏览器重定向到index.php
ob_end_flush();//输出全部内容到浏览器

header()会发送一段文件头给浏览器,但是如果在header()之前已经有了任何输出(包括空输出,比如空格,回车和换行)就会报错。但是如果输出在ob_start()和ob_end_flush()之间,就会没有问题。因为在输出前打开了缓冲区,echo后面的字符就不会输出到浏览器,而是保留在服务器,知道使用flush才会输出,所以header()会正常执行。

当然,ob_start()还可以有参数,参数就是一个回调函数。例子如下:

 代码如下 复制代码

< ? php
function callback($buffer)
{
  // replace all the apples with oranges
  return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
? >
< html >
< body >
<P>It's like comparing apples to oranges.</P>
< / body >
< / html >
< ?php
ob_end_flush();
? >

以上程序会输出:

< html >
< body >
< p>It's like comparing oranges to oranges.</ p>
< / body >
< / html >

至于更多的,就去官网的手册里看吧。

标签:[!--infotagslink--]

您可能感兴趣的文章: