简体   繁体   English

如何从另一个网站以及ip,位置和国家/地区代码中提取数据

[英]How to extract data from another website and ip , location , and country code

I know a site which gives our IP , location and country. 我知道一个提供我们IP,位置和国家/地区的网站。 I don't know how to extract the data and display it in our site. 我不知道如何提取数据并将其显示在我们的网站中。

Source website: https://geoip.nekudo.com/api/ 来源网站: https //geoip.nekudo.com/api/

It outputs data something like this : {"city":false,"country":{"name":"India","code":"IN"},"location":{"accuracy_radius":200,"latitude":13.0833,"longitude":80.2833,"time_zone":"Asia/Kolkata"},"ip":"27.62.5.1"} 它输出类似以下数据:{“ city”:false,“ country”:{“ name”:“ India”,“ code”:“ IN”},“ location”:{“ accuracy_radius”:200,“ latitude” :13.0833, “东经”:80.2833, “TIME_ZONE”: “亚洲/加尔各答”}, “IP”: “27.62.5.1”}

Now I need to get the ip , code, country name in my website. 现在,我需要在网站上获取ip,代码,国家/地区名称。

We can use PHP or JSON, but i need to display the data in site. 我们可以使用PHP或JSON,但是我需要在站点中显示数据。 I know this is a simple question but I'm searching for a long time but still I cant figure out the correct way to scrap and display the result. 我知道这是一个简单的问题,但是我已经搜索了很长时间,但是仍然无法找出正确的方法来抓取并显示结果。

Thanks in advance. 提前致谢。

in a php file simply get file contents and echo the wanted fields 在php文件中,只需获取文件内容并回显所需字段

<p><?php 
        $json = json_decode(file_get_contents("https://geoip.nekudo.com/api/"));
        echo $json->ip;
    ?></p>
    <p><?php
                echo $json->country->code;
        ?></p>
    <p><?php
                echo $json->country->name;
        ?></p>

for a php file without html tags replaced the https with http 对于没有html标记的php文件,将https替换为http

    <?php

$json = json_decode(file_get_contents("http://geoip.nekudo.com/api/"));
echo '<p>' . $json->ip . '</p>';
echo '<p>' . $json->country->code . '</p>';
echo '<p>' . $json->country->name . '</p>';

results in 结果是

<p>77.246.49.14</p><p>ZW</p><p>Zimbabwe</p>

You can make a jQuery AJAX call in a javascript file to fetch that data and store it in your json, then render the json onto your page, like so: 您可以在javascript文件中进行jQuery AJAX调用,以获取该数据并将其存储在json中,然后将json呈现到您的页面上,如下所示:

$.ajax({
  url: "https://geoip.nekudo.com/api/",
  method: "GET",
  success: function(response) {
    $('.some-selector').html(response.ip)
  }
});

and your HTML: 和您的HTML:

<div class="some-selector"></div>

This adds your IP address into the div, as an example. 作为示例,这会将您的IP地址添加到div中。

Assume you make the request to https://geoip.nekudo.com/api/ and returns a variable 'data' To get the ip, code and country name 假设您向https://geoip.nekudo.com/api/发出请求,并返回变量“ data”以获取ip,代码和国家/地区名称

data.ip
data.country.code
data.country.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有共享位置警报的情况下将特定国家/地区 IP 重定向到特定网站登录页面? - How to redirect a particular country IP to a particular website landing page without share location alert? javascript:如何使用正则表达式从网址中提取国家代码 - javascript:How to extract country code from url using regex 如何在react js中使用IP获取国家代码和国家名称 - How to get country code and Country name using IP in react js 我如何从另一个网站的源代码中提取脚本变量的值 - How can i extract a value of a script variable from another website's source code getJSON IP +国家/地区代码 - getJSON IP + country code 如果访客IP来自特定国家/地区,是否可以运行代码 - Is it possible to run code if visitor IP is from a specific country 如何从 JavaScript 字符串数据中提取 IP 地址 - How to extract a IP address from JavaScript string data 如何用其IP位置替换页面中以文本表示的IP地址(例如“国家,地区,城市”) - How to replace IP addresses represented in the page as text with its IP location (as “Country, Region, City” for example) 我该如何编码Discord机器人以从网站提取文字? - How do I code a discord bot to extract text from a website? 如何从拨号代码中推断出国家代码? - How to deduce Country Code from Dial code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM