Get IP detail address using Sina IP Library
<?PHPclasstool{/** * Get IP attribution (Sina IP Library) * * @param $ip String IP Address: 112.65.102.16 * @return Array*/ Static Public functionGetipcity ($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,$match 1); Preg_match("/\" province\ ": \" (. *) \ "/uis",$ipJson,$match 2); Preg_match("/\" city\ ": \" (. *) \ "/uis",$ipJson,$match 3); return Array( ' Country ' =>self::ucode2zh ($match 1[1]),//National' Province ' =>self::ucode2zh ($match 2[1]),//Province' City ' =>self::ucode2zh ($match 3[1])//City ); } /** * Curl way to get information*/ Static Public functionHttpcurl ($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 encoding to Chinese, conversion failed to return original String * * @param $code string Unicode encoding * @return string*/ Static Public functionUcode2zh ($code) { $temp=Explode(' \u ',$code); $rslt=Array(); Array_shift($temp); foreach($temp as $k=$v) { $v=Hexdec($v); $rslt[] = ' a '.$v. ‘;‘; } $r=implode(‘‘,$rslt); return Empty($r) ?$code:$r; }}
Get the IP address class usage instance
<? PHP $ipStr = tool::getipcity (' 112.65.102.16 '); Print_r ($ipStr); # return Results Array ([Country] = China [province] = Shanghai [City] + Shanghai)
Get IP detail address using Sina IP Library