淘宝公司提供了一个很好用的IP地理信息查询接口。 在这里:http://ip.taobao.com/ TaobaoIPQuery2这个类将极大的简化相关的信息查询。 类 TaobaoIPQuery2 文件: 1 ? php 2 /* Usage: 3 * $IPInfo = TaobaoIPQuery2::getIPInfo('IPAddress'); 4 5 http://www.
淘宝公司提供了一个很好用的IP地理信息查询接口。
在这里:http://ip.taobao.com/
TaobaoIPQuery2这个类将极大的简化相关的信息查询。
类 TaobaoIPQuery2 文件:
<span> 1</span> <span>php </span><span> 2</span> <span>/*</span><span> Usage: </span><span> 3</span> <span> * $IPInfo = TaobaoIPQuery2::getIPInfo('IPAddress'); </span><span> 4</span> <span> 5</span> <span>http://www.cnblogs.com/roucheng/ </span><span> 6</span> <span>*/</span> <span> 7</span> <span>Class</span><span> TaobaoIPQuery2{ </span><span> 8</span> <span>private</span> <span>static</span> <span>$_requestURL</span> = 'http://ip.taobao.com/service/getIpInfo.php'<span>; </span><span> 9</span> <span>public</span> <span>static</span> <span>function</span> getIPInfo(<span>$ip</span><span>){ </span><span>10</span> <span>$long</span> = <span>ip2long</span>(<span>$ip</span><span>); </span><span>11</span> <span>if</span>(<span>$long</span> === 0<span>){ </span><span>12</span> <span>throw</span> <span>new</span> <span>Exception</span>('IP address error', 5<span>); </span><span>13</span> <span> } </span><span>14</span> <span>$ip</span>=<span>long2ip</span>(<span>$long</span><span>); </span><span>15</span> <span>$IPInfo</span> = self::queryIPInfo(<span>$ip</span><span>); </span><span>16</span> <span>return</span> self::parseJSON(<span>$IPInfo</span><span>); </span><span>17</span> <span> } </span><span>18</span> <span>19</span> <span>private</span> <span>static</span> <span>function</span> queryIPInfo(<span>$ip</span><span>){ </span><span>20</span> <span>$query</span> = <span>http_build_query</span>(<span>array</span>('ip'=><span>$ip</span><span>)); </span><span>21</span> <span>$ch</span> =<span> curl_init(); </span><span>22</span> <span>$options</span> = <span>array</span><span>( </span><span>23</span> CURLOPT_URL => <span>sprintf</span>('%s?%s', self::<span>$_requestURL</span>, <span>$query</span>), <span>24</span> CURLOPT_RETURNTRANSFER => <span>true</span>, <span>25</span> CURLOPT_AUTOREFERER => <span>false</span>, <span>26</span> CURLOPT_FOLLOWLOCATION => <span>false</span>, <span>27</span> CURLOPT_HEADER => <span>false</span>, <span>28</span> CURLOPT_TIMEOUT => 3.0, <span>29</span> <span> ); </span><span>30</span> curl_setopt_array(<span>$ch</span>, <span>$options</span><span>); </span><span>31</span> <span>$content</span> = curl_exec(<span>$ch</span><span>); </span><span>32</span> curl_close(<span>$ch</span><span>); </span><span>33</span> <span>return</span> <span>$content</span><span>; </span><span>34</span> <span> } </span><span>35</span> <span>36</span> <span>private</span> <span>static</span> <span>function</span> parseJSON(<span>$json</span><span>){ </span><span>37</span> <span>$O</span> = json_decode (<span>$json</span>, <span>true</span><span>); </span><span>38</span> <span>if</span>(<span>false</span> === <span>is_null</span>(<span>$O</span><span>)){ </span><span>39</span> <span>return</span> <span>$O</span><span>; </span><span>40</span> <span> } </span><span>41</span> <span>if</span> (<span>version_compare</span>(<span>PHP_VERSION</span>, '5.3.0', '>='<span>)) { </span><span>42</span> <span>$errorCode</span> =<span> json_last_error(); </span><span>43</span> <span>if</span>(<span>isset</span>(self::<span>$_JSONParseError</span>[<span>$errorCode</span><span>])){ </span><span>44</span> <span>throw</span> <span>new</span> <span>Exception</span>(self::<span>$_JSONParseError</span>[<span>$errorCode</span>], 5<span>); </span><span>45</span> <span> } </span><span>46</span> <span> } </span><span>47</span> <span>throw</span> <span>new</span> <span>Exception</span>('JSON parse error', 5<span>); </span><span>48</span> <span> } </span><span>49</span> <span>50</span> <span>private</span> <span>static</span> <span>$_JSONParseError</span> = <span>array</span><span>( </span><span>51</span> JSON_ERROR_NONE=>'No error has occurred', <span>52</span> JSON_ERROR_DEPTH=>'The maximum stack depth has been exceeded', <span>53</span> JSON_ERROR_CTRL_CHAR=>'Control character error, possibly incorrectly encoded', <span>54</span> JSON_ERROR_STATE_MISMATCH=>'Invalid or malformed JSON', <span>55</span> JSON_ERROR_SYNTAX=>'Syntax error', <span>56</span> JSON_ERROR_UTF8=>'Malformed UTF-8 characters, possibly incorrectly encoded', <span>57</span> <span> ); </span><span>58</span> }
TaobaoIPQuery2.Class.php:
<span> 1</span> <span>php </span><span> 2</span> <span>Class</span><span> TaobaoIPQuery2{ </span><span> 3</span> <span>private</span> <span>static</span> <span>$_requestURL</span> = 'http://ip.taobao.com/service/getIpInfo.php'<span>; </span><span> 4</span> <span>public</span> <span>static</span> <span>function</span> getIPInfo(<span>$ip</span><span>){ </span><span> 5</span> <span>$long</span> = <span>ip2long</span>(<span>$ip</span><span>); </span><span> 6</span> <span>if</span>(<span>$long</span> === 0<span>){ </span><span> 7</span> <span>throw</span> <span>new</span> <span>Exception</span>('IP address error', 5<span>); </span><span> 8</span> <span> } </span><span> 9</span> <span>$ip</span>=<span>long2ip</span>(<span>$long</span><span>); </span><span>10</span> <span>$IPInfo</span> = self::queryIPInfo(<span>$ip</span><span>); </span><span>11</span> <span>return</span> self::parseJSON(<span>$IPInfo</span><span>); </span><span>12</span> <span> } </span><span>13</span> <span>14</span> <span>private</span> <span>static</span> <span>function</span> queryIPInfo(<span>$ip</span><span>){ </span><span>15</span> <span>$query</span> = <span>http_build_query</span>(<span>array</span>('ip'=><span>$ip</span><span>)); </span><span>16</span> <span>$ch</span> =<span> curl_init(); </span><span>17</span> <span>$options</span> = <span>array</span><span>( </span><span>18</span> CURLOPT_URL => <span>sprintf</span>('%s?%s', self::<span>$_requestURL</span>, <span>$query</span>), <span>19</span> CURLOPT_RETURNTRANSFER => <span>true</span>, <span>20</span> CURLOPT_AUTOREFERER => <span>false</span>, <span>21</span> CURLOPT_FOLLOWLOCATION => <span>false</span>, <span>22</span> CURLOPT_HEADER => <span>false</span>, <span>23</span> CURLOPT_TIMEOUT => 3.0, <span>24</span> <span> ); </span><span>25</span> curl_setopt_array(<span>$ch</span>, <span>$options</span><span>); </span><span>26</span> <span>$content</span> = curl_exec(<span>$ch</span><span>); </span><span>27</span> curl_close(<span>$ch</span><span>); </span><span>28</span> <span>return</span> <span>$content</span><span>; </span><span>29</span> <span> } </span><span>30</span> <span>31</span> <span>private</span> <span>static</span> <span>function</span> parseJSON(<span>$json</span><span>){ </span><span>32</span> <span>$O</span> = json_decode (<span>$json</span>, <span>true</span><span>); </span><span>33</span> <span>if</span>(<span>false</span> === <span>is_null</span>(<span>$O</span><span>)){ </span><span>34</span> <span>return</span> <span>$O</span><span>; </span><span>35</span> <span> } </span><span>36</span> <span>if</span> (<span>version_compare</span>(<span>PHP_VERSION</span>, '5.3.0', '>='<span>)) { </span><span>37</span> <span>$errorCode</span> =<span> json_last_error(); </span><span>38</span> <span>if</span>(<span>isset</span>(self::<span>$_JSONParseError</span>[<span>$errorCode</span><span>])){ </span><span>39</span> <span>throw</span> <span>new</span> <span>Exception</span>(self::<span>$_JSONParseError</span>[<span>$errorCode</span>], 5<span>); </span><span>40</span> <span> } </span><span>41</span> <span> } </span><span>42</span> <span>throw</span> <span>new</span> <span>Exception</span>('JSON parse error', 5<span>); </span><span>43</span> <span> } </span><span>44</span> <span>/*</span><span> http://www.cnblogs.com/roucheng/ </span><span>*/</span> <span>45</span> <span>private</span> <span>static</span> <span>$_JSONParseError</span> = <span>array</span><span>( </span><span>46</span> JSON_ERROR_NONE=>'No error has occurred', <span>47</span> JSON_ERROR_DEPTH=>'The maximum stack depth has been exceeded', <span>48</span> JSON_ERROR_CTRL_CHAR=>'Control character error, possibly incorrectly encoded', <span>49</span> JSON_ERROR_STATE_MISMATCH=>'Invalid or malformed JSON', <span>50</span> JSON_ERROR_SYNTAX=>'Syntax error', <span>51</span> JSON_ERROR_UTF8=>'Malformed UTF-8 characters, possibly incorrectly encoded', <span>52</span> <span> ); </span><span>53</span> }
调用:
<span>$ip</span> = <span>$_SERVER</span>["REMOTE_ADDR"<span>]; </span><span>$ipquery</span> = <span>new</span> taobaoIPQuery(<span>$ip</span><span>); </span><span>$region</span> = <span>$ipquery</span>-><span>get_region(); </span><span>$country</span> = <span>$ipquery</span>-><span>get_country(); </span><span>$city</span> = <span>$ipquery</span>->get_city();
另外新浪也提供接口:http://ipapi.sinaapp.com/