Get IP detail address using Sina IP Library

Source: Internet
Author: User
Tags get ip

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.