【问题标题】:Ip location finderIP位置查找器
【发布时间】:2015-05-24 14:05:01
【问题描述】:

我想通过我的网站访问者的 IP 找到他们的位置(国家、城市、..)。 我正在编写php。 谁能帮我? 像这样:

   $url = json_decode(file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=/*userapikey*/
    ip=".$_SERVER['REMOTE_ADDR']."&format=json"));
    $country=$url->countryName;  // user country
    $city=$url->cityName;       // city
    $region=$url->regionName;   // regoin
    $latitude=$url->latitude;    //lat and lon
    $longitude=$url->longitude;

【问题讨论】:

    标签: php location ip


    【解决方案1】:

    网址中似乎缺少&

    $url = json_decode(file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=/*userapikey*/&ip=".$_SERVER['REMOTE_ADDR']."&format=json"));
    

    或许,您应该对 IP 进行编码:

    $url = json_decode(file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=/*userapikey*/&ip=".urlencode($_SERVER['REMOTE_ADDR'])."&format=json"));
    

    然后您可以这样做以获取有关结果的信息:

    var_dump($url);
    

    如果您还有其他问题,请写到问题中。

    【讨论】:

    • 结果如何?!请阅读 Stack Overflow 的常见问题解答。您的问题和此评论非常不具体。
    【解决方案2】:

    看看freegeoip.net。它是一种网络服务,可为您提供特定 IP 所需的准确数据。

    例如:

    https://freegeoip.net/json/1.2.3.4
    

    会给你这个 JSON 数据:

    {
        ip: "1.2.3.4",
        country_code: "US",
        country_name: "USA",
        region_code: "WA",
        region_name: "Washington",
        city: "Mukilteo",
        zip_code: "98275",
        time_zone: "America/Los_Angeles",
        latitude: 47.945,
        longitude: -122.305,
        metro_code: 819
    }
    

    【讨论】:

      【解决方案3】:

      查看https://freegeoip.net/。他们提供免费的 API,每小时最多可进行 10,000 次搜索。


      例子:

      $res         = json_decode(file_get_contents("https://freegeoip.net/json/109.80.75.20"));
      $countryCode = $res->country_code;
      $country     = $res->country_name;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-05
        • 2013-11-15
        • 2022-11-05
        • 2011-11-25
        • 2014-11-06
        • 2013-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多