根据IP地址将位置保存到数据库
我正在尝试根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因此,根据 ipinfodb,您必须注册 API 密钥。但他们有一个 API 类,即
getCountry($host)在您提供
将获取 IP 所在的国家/地区。或者,如果您不想使用它,您可以使用他们的 XML 或 JSON API。protected $apiKey = '';
您的 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 theprotected $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)));
.简单地使用 $data['country'] 是行不通的,因为给定的结果不是对象或数组。您需要将内容拆分为一个数组,或者更好的是,将其作为 JSON 对象检索,然后使用:
为您提供数组中的数据。
尽管如此,我不建议从 IP 地址获取位置,因为它非常不准确!它返回主机服务器的位置,不一定是用户的位置。
编辑:
使用 JSON API:http://ipinfodb.com/ip_location_api_json.php
更新:
实施 JSON API 的步骤:
复制并粘贴以下 PHP 代码:
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:
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:
Copy and past the following PHP code:
您可以使用换行符分隔符拆分为一个数组,然后使用一系列字符串操纵器对其进行清理...或者您可以找到一个将数据作为 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.
你可以创建一个像这样的数组:
然后序列化该数组并通过IP地址将其保存在数据库中,然后你就只有2个表列:
然后你可以使用一个函数从数据库检索数据并通用化从信息列检索的数组。有关 php 序列化 的更多信息
you can make an array like this :
and then serialize the array and save it via ip address in database and then you just have 2 table column :
then you can use a function to retrieve data from db and universalize the array retrieved from info column.more information about php serialze