码迷,mamicode.com
首页 > 其他好文 > 详细

ip接口调用

时间:2015-07-12 00:04:20      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

<?php
header("Content-type: text/html; charset=utf-8");

function getIP(){

    if (isset($_SERVER)) {

        if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) {

            $realip = $_SERVER[HTTP_X_FORWARDED_FOR];

        } elseif (isset($_SERVER[HTTP_CLIENT_IP])) {

            $realip = $_SERVER[HTTP_CLIENT_IP];

        } else {

            $realip = $_SERVER[REMOTE_ADDR];

        }

    } else {

        if (getenv("HTTP_X_FORWARDED_FOR")) {

            $realip = getenv( "HTTP_X_FORWARDED_FOR");

        } elseif (getenv("HTTP_CLIENT_IP")) {

            $realip = getenv("HTTP_CLIENT_IP");

        } else {

            $realip = getenv("REMOTE_ADDR");

        }

    }

    return $realip;

}



$ip = getIP();



// 利用新浪接口根据ip查询所在区域信息

/* $res0 = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip");

$res0 = json_decode($res0);

print_r($res0);

echo "<br />"; */



// 利用淘宝接口根据ip查询所在区域信息

$res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");

$res1 = json_decode($res1);

/* print_r($res1); */





$array = get_object_vars($res1);//赋值给数组

foreach($array as $value){

//    echo $value->region."<br />";

    echo $value->city."<br />";

//    echo $value->ip."<br />";



}

$getcity = $array;

if($getcity == "泰安市"){ echo "QQ:691228813"; }

else{echo "QQ:123456";}





//或者用对象数据访问 echo $res1->data->city;



//$address = array("city");

//if(false!==strpos($address,‘泰安市‘)){

////    header(‘Location:http://www.daimajiayuan.com/?city=山东‘);

//    echo "联系QQ:691228813";

//}else if(false!==strpos($address,‘北京‘)){

////    header(‘Location:http://www.daimajiayuan.com/?city=北京‘);

//    echo "联系QQ:123456";

//

//}









?>

 

ip接口调用

标签:

原文地址:http://www.cnblogs.com/voidcat/p/4639495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!