Use the Sina IP library to obtain the detailed IP address
Use the Sina IP library to obtain the detailed IP address
- Class 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 information retrieval
- */
- 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, CURLOPT_RETURNTRANSFER, 1 );
- Curl_setopt ($ curl_handle, CURLOPT_FAILONERROR, 1 );
- $ File_content = curl_exec ($ curl_handle );
- Curl_close ($ curl_handle );
- Return $ file_content;
- }
- /**
- * Convert unicode 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
- $ IpStr = Tool: getIpCity ('192. 65.102.16 ');
- Print_r ($ ipStr );
- # Returned results
- Array ([country] => China [province] => Shanghai [city] => Shanghai)
|