PHP code for obtaining the IP address location program-PHP source code

Source: Internet
Author: User
PHP uses related functions and regular expressions to obtain information on a specified webpage when obtaining IP addresses. Below I will sort out several common interfaces and obtaining methods, for more information, see. PHP uses related functions and regular expressions to obtain information on a specified webpage when obtaining IP addresses. Below I will sort out several common interfaces and obtaining methods, for more information, see.

Script ec (2); script

Use php file_get_contents to obtain the IP address and obtain the geographic location. See the following code:

Use file_get_contents and fopen to enable allow_url_fopen. Method: Edit php. ini and set allow_url_fopen = On. When allow_url_fopen is disabled, neither fopen nor file_get_contents can open remote files.

Example

The Code is as follows:

Function get_ip_place ()

{
$ Ip = file_get_contents ("http://fw.qq.com/ipaddress ");
$ Ip = str_replace ('"','', $ ip );
$ Ip2 = explode ("(", $ ip );
$ A = substr ($ ip2 [1], 0,-2 );
$ B = explode (",", $ );
Return $ B;
}


There is another way:

Let's see.

The Code is as follows:
Function get_ip_arr ()
{
$ Ip = file_get_contents ("http://fw.qq.com/ipaddress ");
Preg_match_all ("/" (. *) "/", $ ip, $ arr );
Return $ arr;
}

The returned result is an array, which can be any region or ip address.

/Use curl:

Use curl to enable curl. Method: Modify php. ini in windows, remove the semicolon before extension = php_curl.dll, and copy ssleay32.dll and libeay32.dll to C:/WINDOWS/system32. Install curl extension in Linux.

Example

The Code is as follows:

Function getIPLoc ($ queryIP ){
$ Url = 'HTTP: // ip.qq.com/cgi-bin/searchip? Searchip1 = '. $ queryIP;
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_ENCODING, 'gb2312 ');
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 10 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // get data and return
$ Result = curl_exec ($ ch );
$ Result = mb_convert_encoding ($ result, "UTF-8", "gb2312"); // code conversion, otherwise garbled
Curl_close ($ ch );
Preg_match ("@(.*)

@ IU ", $ result, $ ipArray );
$ Loc = $ ipArray [1];
Return $ loc;
}

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.