PHP obtains the detailed IP address based on the Sina IP address library.

Source: Internet
Author: User

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

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.