PHP获取ip与ip所在城市

1获取真实ip,本地测试总是::1 或者127.0.0.1 或者局域网的ip

/**
 * 获取用户真实 IP
 */
function getIP()
{
    static $realip;
    if (isset($_SERVER)){
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (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");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        } else {
            $realip = getenv("REMOTE_ADDR");
        }
    }
    return $realip;
}
 

2.根据ip获取地址

/**
 * 获取 IP  地理位置
 * 淘宝IP接口
 * @Return: array
 */
function getCity($ip = ‘‘)
{
    if($ip == ‘‘){
        $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
        $ip=json_decode(file_get_contents($url),true);
        $data = $ip;
    }else{
        $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;
}

测试

var_dump(getIP());
var_dump(getCity());
var_dump(getCity(‘218.93.250.162‘));

结果

string(3) "::1"
array(10) {
  ["ret"]=>
  int(1)
  ["start"]=>
  int(-1)
  ["end"]=>
  int(-1)
  ["country"]=>
  string(6) "中国"
  ["province"]=>
  string(6) "江苏"
  ["city"]=>
  string(6) "宿迁"
  ["district"]=>
  string(0) ""
  ["isp"]=>
  string(0) ""
  ["type"]=>
  string(0) ""
  ["desc"]=>
  string(0) ""
}
array(13) {
  ["country"]=>
  string(6) "中国"
  ["country_id"]=>
  string(2) "CN"
  ["area"]=>
  string(6) "华东"
  ["area_id"]=>
  string(6) "300000"
  ["region"]=>
  string(9) "江苏省"
  ["region_id"]=>
  string(6) "320000"
  ["city"]=>
  string(9) "宿迁市"
  ["city_id"]=>
  string(6) "321300"
  ["county"]=>
  string(0) ""
  ["county_id"]=>
  string(2) "-1"
  ["isp"]=>
  string(6) "电信"
  ["isp_id"]=>
  string(6) "100017"
  ["ip"]=>
  string(14) "218.93.250.162"
}

说明:新浪的接口,直接能获取到地址信息,淘宝的接口需要提供ip,不过获取的信息更全面

转载:https://www.cnblogs.com/jiqing9006/p/4994728.html#commentform

原文地址:https://www.cnblogs.com/qq1069284034/p/8410624.html

时间: 2024-07-30 09:18:36

PHP获取ip与ip所在城市的相关文章

php 获取客户端IP地址经纬度所在城市

1. [代码]获取客户端IP地址经纬度所在城市 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php   $getIp=$_SERVER["REMOTE_ADDR"];   echo 'IP:',$getIp;   echo '<br/>';   $content = file_get_contents("http://api.map.baidu.com/location/ip?ak=7IZ6fgGEGohCrRKUE9Rj4TSQ&a

python中通过客户端IP拿到所在城市和当地天气信息—附带项目案例

熟悉老一代QQ的小伙伴可能都知道,很早以前的QQ,鼠标滑到头像的位置,你的位置和IP会在详情页显示,那么这个是如何做到的呢?下面我们就来玩一玩这个东西 首先,需求分析: 1.拿到客户端IP 2.通过IP拿到客户端所在地区 3.通过地区拿到当地天气 4.整合功能,展示给用户 第一步,如何拿到用户IP 我们以Django环境为例 # 客户端的请求,IP信息会在请求头中 request.META['REMOTE_ADDR'] # 或 request.META.get('HTTP_X_FORWARDED

获取客户端IP地址经纬度所在城市

<?php $getIp=$_SERVER["REMOTE_ADDR"]; echo 'IP:',$getIp; echo '<br/>'; $content = file_get_contents("http://api.map.baidu.com/location/ip?ak=7IZ6fgGEGohCrRKUE9Rj4TSQ&ip={$getIp}&coor=bd09ll"); $json = json_decode($cont

PHP中根据IP地址判断所在城市等信息

本篇文章由:http://xinpure.com/php-based-on-information-such-as-the-ip-address-in-your-city/ 获得IP地址 在 PHP 中得到当前访问者的IP地址,还是比较简单的: $ip = $_SERVER['REMOTE_ADDR'] 将IP转换为城市等信息 淘宝提供了一个IP数据接口: http://ip.taobao.com/service/getIpInfo.php?ip=ip地址 $response = file_ge

PHP 根据IP地址获取所在城市

有这样的需求,需要根据用户的IP地址,定位用户所在的城市. 本文记录性文章,无逻辑性.有这样需求的朋友,可以直接拷贝使用.直接上代码,不需赘述. <?php header('Content-Type:text/html;Charset=utf-8'); function GetIp(){ $realip = ''; $unknown = 'unknown'; if (isset($_SERVER)){ if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&

php 通过ip获取所在城市地址信息 获取计算机外网ip

<!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-

js根据ip地址获取所在城市

话不多说,直接贴代码! 将这段代码运行一下就OK了: <html xmlns=""> <head runat="server"> <title>根据IP地址获取所在城市</title> <scriptsrc="?format=js"type="text/ecmascript"></script> <script type="text/jav

获取ip,判断用户所在城市

PHP获取IP地址 这个比较简单了,利用PHP自带函数就可以了,PHP中文手册看一下,都有现成的例子,就不过多说明了,直接上代码,A段: <? //PHP获取当前用户IP地址方法 $xp_UserIp = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"]; $xp_UserIp = ($xp_UserIp) ? $xp_Us

C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9,其中ak是密钥,自行去申请即可,便于大家测试,楼主就公布并了自己的Key,这样可以直接获取到数据. 获取到的数据是这样的: {"error":0,"status":"success","date"

ip获取所在城市名称等信息接口,及函数

函数: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 function https_request($url,$data = null){     $curl = curl_init();          curl_setopt($cur