A brief summary of several methods of obtaining IP geographic information by comparison-related skills

Source: Internet
Author: User
Tags curl explode fread
The first is the use of pure IP database, this can be found on the Internet a lot, the disadvantage is that the update a bit slow.
The second is to use the interface of the portal site
Currently known Tencent, Sina, NetEase, Sohu and Google to provide IP address query API, but find only Tencent, Sina and NetEase, Google's seemingly to use Google Maps so no research. Looked at the domestic several Tencent provided is JavaScript, NetEase provides is XML, and Sina has a variety of formats can be used, note that non-XML data sources are GBK format, whether it is JavaScript calls or PHP calls to convert the code, or get garbled. And more attention is that, if a one-time query multiple IP, using the Portal API to query will be very slow, I probably wrote a For loop to try, whether it is in the PHP parsing xml or file_get_contents () function to get content, query more than 10 times will become very slow, may even timeout.
Tencent IP Address API interface address: Http://fw.qq.com/ipaddress, returned is the data format: var ipdata = new Array ("123.124.2.85″," "," Beijing, ""); A JavaScript object that does not yet know how to enter an IP query.
Sina's IP address query interface: HTTP://INT.DPOOL.SINA.COM.CN/IPLOOKUP/IPLOOKUP.PHP?FORMAT=JS
Sina Multi-Region test method: http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=123.124.2.85
NetEase Youdao IP Address query interface: http://www.youdao.com/smartresult-xml/search.s?type=ip&q=123.124.2.85
Use the JS code to transfer the API interface of Tencent:
View Source code Printing Help
Copy Code code as follows:

<script language= "javascript" type= "Text/javascript" src= "http://fw.qq.com/ipaddress" ></script>
<script>document.write ("Your IP is:" +ipdata[0]+ ", From:" +ipdata[2]);</script>
The PHP calling method of Tencent API
function Getipplace () {
$ip =file_get_contents ("http://fw.qq.com/ipaddress");
$ip =str_replace (' ",", $IP);
$ip 2=explode ("(", $IP);
$a =substr ($ip 2[1],0,-2);
$b =explode (",", $a);
return $b;
}
$ip =getipplace ();
Print_r ($IP);
Call query interface need to crawl Web page, there are three ways, the first is curl, the second is
File_get_contents, third Fopen->fread->fclose, recommend the second method
/*
* Based on Tencent IP sharing program address to obtain IP location, more accurate
*/
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 back
$result = curl_exec ($ch);
$result = mb_convert_encoding ($result, "Utf-8″," Gb2312″); Code conversion, otherwise garbled
Curl_close ($ch);
Preg_match ("@<span> (. *) </span></p> @iU", $result, $ipArray);
$loc = $ipArray [1];
return $loc;
}
Query IP address According to Tencent interface, use file_get_contents to grab Web page
function Getiploc ($queryIP) {
$url = ' http://ip.qq.com/cgi-bin/searchip?searchip1= '. $queryIP;
$result = file_get_contents ($url);
$result = mb_convert_encoding ($result, "Utf-8″," Gb2312″); Code conversion, otherwise garbled
Preg_match ("@<span> (. *) </span></p> @iU", $result, $ipArray);
$loc = $ipArray [1];
return $loc;
}
Query IP address According to Tencent interface, use Fopen->fread->fclose to grab Web page
function Getiploc ($queryIP) {
$url = ' http://ip.qq.com/cgi-bin/searchip?searchip1= '. $queryIP;
$handle = fopen ("$url", "RB");
$result = "";
do {
$data = Fread ($handle, 1024);
if (strlen ($data) = = 0) {
Break
}
$result. = $data;
} while (true);
$result = mb_convert_encoding ($result, "Utf-8″," Gb2312″); Code conversion, otherwise garbled
Preg_match ("@<span> (. *) </span></p> @iU", $result, $ipArray);
$loc = $ipArray [1];
return $loc;
}
/******** Note:
1. Use file_get_contents and fopen to open allow_url_fopen. Methods: Edit PHP.ini, set allow_url_fopen = on,allow_url_fopen cannot open remote files when fopen and file_get_contents are closed.
2. Use curl must be open curl space. Method: Modify PHP.ini under WINDOWS, remove the semicolon in front of Extension=php_curl.dll, and need to copy Ssleay32.dll and Libeay32.dll to C:\WINDOWS\system32 ; Linux to install the Curl extension *****/
Sina Query IP Interface Fifth sixth is geographical information
function Getiploc ($IP _ip) {
$IP _str = @file_get_contents (' http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip= '. $IP _ip);
if (!empty ($IP _str)) {
$IP _tmp = Explode ("", $IP _str);
$IP _city = Iconv ("GBK", "Utf-8″, $IP _tmp[5]");
return $IP _city;
}
Youdao API method of invoking PHP
$url = "http:www.youdao.com/smartresult-xml/search.s?type=ip&q=". $ip;
$doc = new DOMDocument ();
$doc->load ($url);
$smartresult = $doc->getelementsbytagname ("Product");
foreach ($smartresult as $product)
{
$locations = $product->getelementsbytagname ("location");
$location = $locations->item (0)->nodevalue;
}
if ($location!= "")
{
echo $i. "." $ip;
echo "from". $location. " of netizens ";
}
Else
{
echo $i. "." $ip;
echo "A netizen from Mars";
}
Public Function Sinaipapi ($IP) {
$str = file_get_contents ("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=". $ip);
$str = Iconv ("GBK", "Utf-8//ignore", $str);
Preg_match_all ("/[\x{4e00}-\x{9fa5}]+/u", $str, $get);
$add = Implode (", $get [0]);
return $add;
}
$get is a very good two-dimensional array.

One of the Youdao and Sina is my own writing, Sina API can also be like Tencent API with the file_get_contents () function to obtain the address after the use of a series of string function processing, I wrote the function using regular expressions from Sina's return results to provide a string containing Chinese, and to deposit a two-dimensional array, this may only be useful for the Sina API and there are bugs. For example, after the IP address assigned to me by the school var_dump () the $get variable in the function gets the following result: Array (1) {[0]=> Array (6) {[0]=> string (6) "China" [1]=> string ( 6 "Beijing" [2]=> string (6) "Beijing" [3]=> string (9) "Education Network" [4]=> string (6) "School" [5]=> string (18) "Geosciences"}}, and the result of the function output Is "China Beijing Beijing Education Network School Geosciences", I hope that my ideas and methods can be useful to others.
Finally again, if it is WordPress please use the first method, otherwise use the API at the same time to query all the real address of the message will allow PHP timeout, I hope that all the way Daniel has a better way, as to limit the display and display methods such as God Horse is a wordpress application problem, At the same time for Java and C #, the idea is the same, these follow-up questions and so I finished the test and then elaborate.

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.