PHP obtains the detailed IP address based on the Sina IP address library.
This example describes how PHP obtains the detailed IP address based on the Sina IP address library. We will share this with you for your reference. The details are as follows:
<? Phpclass Tool {/*** obtain the IP address location (sina ip library) ** @ param $ ip String ip Address: 112.65.102.16 * @ return Array */static public function getIpCity ($ IP) {$ ip = preg_replace ("/\ s/", "", preg_replace ("/\ r \ n/", "", $ ip )); $ link = "http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & ip = ". $ ip. "& t = ". time (); $ ipJson = self: httpCurl ($ link); preg_match ("/\" country \":\"(. *) \ "/Uis", $ ipJson, $ mattings); preg_match ("/\" province \":\"(. *) \ "/Uis", $ ipJson, $ match2); preg_match ("/\" city \":\"(. *) \ "/Uis", $ ipJson, $ match3); return array ('country' => self: ucode2zh ($ mattings [1]), // country 'province '=> self: ucode2zh ($ match2 [1]), // province 'city' => self: ucode2zh ($ match3 [1]) // City);}/*** Curl Method Get information */static public function httpCurl ($ url) {$ curl_handle = curl_init (); curl_setopt ($ curl_handle, CURLOPT_URL, $ url); curl_setopt ($ curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt ($ curl_handle, handle, 1); curl_setopt ($ curl_handle, CURLOPT_FAILONERROR, 1); $ file_content = curl_exec ($ curl_handle); curl_close ($ curl_handle ); return $ file_content;}/*** converts unicode encoding to Chinese. If conversion fails, the original string is returned. ** @ Param $ code String unicode encoding * @ return String */static public function ucode2zh ($ code) {$ temp = explode ('\ U', $ code ); $ rslt = array (); array_shift ($ temp); foreach ($ temp as $ k => $ v) {$ v = hexdec ($ v ); $ rslt [] = '&#'. $ v. ';} $ r = implode ('', $ rslt); return empty ($ r )? $ Code: $ r ;}}
Get IP Address class use instance
<?php$ipStr = Tool::getIpCity('112.65.102.16');print_r($ipStr);
Returned results
Array ([country] => China [province] => Shanghai [city] => Shanghai)
PS: Here are several IP address related online tools for your reference:
IP address attribution online query tool:
Http://tools.jb51.net/aideddesign/ipcha
Online Network calculator | TCP/IP subnet mask calculation and Conversion Tool:
Http://tools.jb51.net/aideddesign/ipcalc
Online IP Address/subnet mask calculation and conversion tools:
Http://tools.jb51.net/aideddesign/ip_net_calc
Online subnet mask conversion and network computing tools:
Http://tools.jb51.net/aideddesign/network_calc