<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> <Title> obtain the region through IP address </title> </Head> Figure 1: returned data
Figure 2: query successful
If you like it, you can try it and get a few lines of code. I think it is quite practical !!
This article from the generation of Ruibo customer original address: http://www.daixiaorui.com/read/3.html <Body> <Form action = "" method = "post"> <P> enter an ip address: <input type = "text" name = "ip"/> </p> <P> <input type = "submit" value = "query"/> </p> </Form> <? Php // Obtain the IP address // $ Ip = $ _ SERVER ['remote _ ADDR ']; // automatically obtains the IP address of the client. // Region corresponding to the ip address If (! Empty ($ _ POST ['IP']) { $ Ip = $ _ POST ['IP']; // Interface address, which returns an xml result set, as shown in Figure 1; $ Str = file_get_contents ("http://www.yodao.com/smartresult-xml/search.s? Type = ip & q = ". $ ip ); // Here we need to get the address information. There are many xml extraction methods, and I use regular expressions. Preg_match_all ("/<location> (.*?) </Location>/", $ str, $ addr1 ); // The final result, as shown in figure 2; $ Addr = $ ip. "=>". $ addr1 [1] [0]; Echo $ addr; } ?> </Body> </Html> |