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; } |