[英]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.