根据IP地址将位置保存到数据库

发布于 2024-12-29 18:11:21 字数 714 浏览 0 评论 0原文

我正在尝试根据 IP 地址使用 hostip 来保存到数据库用户的一般区域/位置。但是我不确定如何解析我收到的输出中的数据。

    <?php
        $ip = $_SERVER['REMOTE_ADDR'];
        $data = json_decode(file_get_contents("http://api.hostip.info/get_html.php?ip=$ip")); //<-not sure how
        $country = $data['Country'];
        $city = $data['City']; 
    ?>

您可以在这里看到我如何接收数据: http://api.hostip.info/get_html.php?ip=00.0。 00.00

另一种选择是使用此处的第一个答案(使用 ipinfodb):从 PHP 中的 IP 获取位置详细信息,但它甚至不显示任何信息数据,即使我尝试在url中插入我的IP,更不用说解析结果了,所以我改成上面的。

I am trying to use hostip in order to save to DB user's general area/ location, according to IP address. However I am not sure how to parse the data from the output I am receiving.

    <?php
        $ip = $_SERVER['REMOTE_ADDR'];
        $data = json_decode(file_get_contents("http://api.hostip.info/get_html.php?ip=$ip")); //<-not sure how
        $country = $data['Country'];
        $city = $data['City']; 
    ?>

You can see here how I am receiving the data:
http://api.hostip.info/get_html.php?ip=00.0.00.00

Another option was to use the first answer here (using ipinfodb): Getting location details from IP in PHP, but it doesn't even display any data, even when I try inserting my IP in the url, let alone parse the results, so I changed to the above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玻璃人 2025-01-05 18:11:21

因此,根据 ipinfodb,您必须注册 API 密钥。但他们有一个 API 类,即 getCountry($host)在您提供 protected $apiKey = ''; 您的 API 密钥后, 将获取 IP 所在的国家/地区。或者,如果您不想使用它,您可以使用他们的 XML 或 JSON API。

更新 1

或者,如果这对您来说太难,请在其他服务的换行符 (\n) 上使用爆炸。或者,也许使用其他服务。

更新2

似乎您链接的问题中提到的geoPlugin确实不需要 API 密钥。他们使用的代码是 echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip)));

So, according to ipinfodb, you will have to sign up for an API key. But they have an API class, which's getCountry($host) will get the country of the IP after you gave the protected $apiKey = ''; your API key. Or if you don't want to use that, you can use their XML or their JSON API.

UPDATE 1

Or, if that is too hard for you, then use explode on the newline separator (\n) at the other service. Or, perhaps use another service.

UPDATE 2

Seems that geoPlugin, mentioned in the question you linked does not require an API key. Their code to use is echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip)));.

零崎曲识 2025-01-05 18:11:21

简单地使用 $data['country'] 是行不通的,因为给定的结果不是对象或数组。您需要将内容拆分为一个数组,或者更好的是,将其作为 JSON 对象检索,然后使用:

$data = json_decode($data);

为您提供数组中的数据。

尽管如此,我不建议从 IP 地址获取位置,因为它非常不准确!它返回主机服务器的位置,不一定是用户的位置。

编辑:

使用 JSON API:http://ipinfodb.com/ip_location_api_json.php

更新:

实施 JSON API 的步骤:

  1. 在此处注册 API 密钥:http://ipinfodb.com/register.php。这将允许您从他们的服务器检索结果,否则它将无法工作。
  2. 您不需要实现任何 SDK 即可使其工作,因此您需要做的就是:

复制并粘贴以下 PHP 代码:

// Set the parameters
$ip_address = $_SERVER['REMOTE_ADDR'];
$api_key = 'YOUR_API_KEY_HERE';

// Get the data
$data = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=$api_key&ip=$ip_address&format=json");

// Decode the JSON result into an array
$data = json_decode($data);

// All data can now be accessed using the associative array $data
$country = $data['Country']; // Country
$city = $data['City']; // City

Simply using $data['country'] will not work because the given result is not an object or array. You need to split the content into an array or, even better, retrieve it as a JSON object and then use:

$data = json_decode($data);

To give you the data in an array.

Despite the above, I wouldn't advise getting locations from IP Addresses because it is incredibly inaccurate! It returns the location of the Host server, not necessarily the location of the user.

EDIT:

Use the JSON API: http://ipinfodb.com/ip_location_api_json.php

Update:

Steps to implementing the JSON API:

  1. Register for an API key here: http://ipinfodb.com/register.php. This will allow you to retrieve results from their server, without this it will not work.
  2. You do not need to implement any SDK to get this to work, so all you should need is this:

Copy and past the following PHP code:

// Set the parameters
$ip_address = $_SERVER['REMOTE_ADDR'];
$api_key = 'YOUR_API_KEY_HERE';

// Get the data
$data = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=$api_key&ip=$ip_address&format=json");

// Decode the JSON result into an array
$data = json_decode($data);

// All data can now be accessed using the associative array $data
$country = $data['Country']; // Country
$city = $data['City']; // City
心意如水 2025-01-05 18:11:21

您可以使用换行符分隔符拆分为一个数组,然后使用一系列字符串操纵器对其进行清理...或者您可以找到一个将数据作为 json 返回的服务,这将完全减轻完成所有这些操作的痛苦。

You can split into an array with a newline separator, and then scrub it with a series of string manipulators... or you can find a service that will return the data as json, which will totally ease the pain of going through all of that.

狂之美人 2025-01-05 18:11:21

你可以创建一个像这样的数组:

array(
   array(
        'ip' => '0.0.0.0',
        'country' => 'someplace',
        'city' => 'somecity'm ,
        'data' => 'somedata'),
     array(
        'ip' => '0.0.0.0',
        'country' => 'someplace',
        'city' => 'somecity'm ,
        'data' => 'somedata'),
.
.
. );

然后序列化该数组并通过IP地址将其保存在数据库中,然后你就只有2个表列:

ip ::: info 

然后你可以使用一个函数从数据库检索数据并通用化从信息列检索的数组。有关 php 序列化 的更多信息

you can make an array like this :

array(
   array(
        'ip' => '0.0.0.0',
        'country' => 'someplace',
        'city' => 'somecity'm ,
        'data' => 'somedata'),
     array(
        'ip' => '0.0.0.0',
        'country' => 'someplace',
        'city' => 'somecity'm ,
        'data' => 'somedata'),
.
.
. );

and then serialize the array and save it via ip address in database and then you just have 2 table column :

ip ::: info 

then you can use a function to retrieve data from db and universalize the array retrieved from info column.more information about php serialze

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文