<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; ip</title>
	<atom:link href="https://www.phppan.com/tag/ip/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 25 Apr 2026 00:56:17 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>通过IP定位经纬度</title>
		<link>https://www.phppan.com/2009/10/latitude-and-longitude-location-by-ip/</link>
		<comments>https://www.phppan.com/2009/10/latitude-and-longitude-location-by-ip/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 01:16:55 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ip]]></category>
		<category><![CDATA[定位经纬度]]></category>
		<category><![CDATA[经纬度]]></category>
		<category><![CDATA[通过IP定位经纬度]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=272</guid>
		<description><![CDATA[由于需要给客户端提交数据接口并且数据中包括经纬度，然而数据库中只存储了IP，于是需要通过IP定位经纬度。 一开 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>由于需要给客户端提交数据接口并且数据中包括经纬度，然而数据库中只存储了IP，于是需要通过IP定位经纬度。</p>
<p>一开始查找是否存在Google API直接将IP转化成经纬度，没有，只有将地址转化成经纬度，开始其它查找，得到方案一和方案二：<br />
【方案一】<br />
<a href="http://www.cnblogs.com/step1/archive/2009/01/07/1371402.html">使用闭关纪要21.地图人站点的IP查询经纬度和经纬度查询行政区划的服务 </a><br />
地址为：<br />
<a href="http://dituren-service.appspot.com/services/ip_lookup?c=onIpLookupLoaded&#038;ip=24.24.24.24">http://dituren-service.appspot.com/services/ip_lookup?c=onIpLookupLoaded&#038;ip=24.24.24.24</a><br />
其实现思路为：采用IP库实现了通过IP得到一个地址的文字描述之后，再通过Google的地址解析服务去匹配经纬度，调用代码如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?PHP</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://dituren-service.appspot.com/services/ip_lookup?c=onIpLookupLoaded&amp;ip=24.24.24.24&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/\((.*)\)/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>【方案二】<br />
使用http://www.blueforge.net/map/ 提供的IP地址查询服务<br />
取数据代码如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?PHP</span>
<span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rst</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.blueforge.net/map/?rs=get_ip_info&amp;rst=&amp;rsrnd=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$rst</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;rsargs[]=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ip</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rs</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/{(.*)}/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$arr=array('</span> <span style="color: #339933;">.</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'=&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">');'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'parseFloat'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'floatVal'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>由于作者是直接给JS用的，所以源地址返回的数据是JS代码，我在这里做了一个替换，将其变为PHP代码。<br />
以上两种方法都是基于Google API，并且在速度和稳定性方面存在极大的隐患</p>
<p>【方案三】<br />
比较好的方案是在本地有一个数据库存储了相关信息，直接取出来，<br />
http://www.maxmind.com/ 这个网站提供了这样一个数据库，在其城市级的数据中，我们可以找到经纬度信息，网站提供了各种语言的程序实现，这里就不再多说了。<br />
数据库下载地址：<br />
<a href="http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz">http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz</a><br />
PHP程序下载地址：<br />
<a href="http://geolite.maxmind.com/download/geoip/api/php/">http://geolite.maxmind.com/download/geoip/api/php/</a></p>
<p>方案三存在两个问题：<br />
<strong><br />
1、一些数据不全<br />
2、存在一些乱码，特别是城市相关信息<br />
</strong></p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/10/latitude-and-longitude-location-by-ip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
