Call Sina interface, get computer IP, province city

Source: Internet
Author: User
Tags explode

Previously found the way to invoke the Tencent interface,

[PHP] Http://blog.csdn.net/a7758521haha/archive/2011/05/11/6411620.aspx

[JavaScript] Http://blog.csdn.net/a7758521haha/archive/2011/05/09/6406287.aspx

Try to write down using the Sina interface to get location information way, error is full, now write this article, put my coding ideas, encountered problems, as well as solutions to the collation summarized as follows:

First Open Sina's interface Http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js

is an array, there is similar to/u4e2d/u56fd such content, I thought of using PHP to implement the Tencent interface call method, split the decomposition array, and Iconv function conversion code, and then do, write the code as follows:

<?php @header ("content-type:text/html; charset:gb2312 "); function Getipplace () {$ip = file_get_contents ("Http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"); Echo $ip; echo "<br/>"; $ip = Iconv ("GB2312", "UTF-8", $IP); Echo $ip; echo "<br/>"; $ip = Str_replace (' ",", $IP); Echo $ip; echo "<br/>"; $ip 2 = Explode ("{", $ip); Print_r ($ip 2); echo "<br/>"; $a = substr ($ip 2[1],0,-2); echo $a; echo "<br/>"; $b = Explode (",", $a); Print_r ($b); echo "<br/>"; $str = Json_encode ($b); Print_r ($STR); return $str; }?> <mce:script type= "Text/javascript" ><!--var arr = <?php echo getipplace ();?>; document.write (Arr[3]); --></mce:script>

Display Result: Country:/U4E2D/U56FD

Did not get the Chinese display, and then check the code without fruit, a sentence returned results did not know the reason, so in the forum post help, get a lot of various solutions, (PS:CSDN forum is really master clouds, crouching Tiger Hidden Dragon),

The first advice is to use htmlencode, I searched the Internet for the relevant articles, the conclusions are as follows:
1. Some articles are introduced JS implementation of the garbled conversion is implemented through JSP, I do not understand the JSP
2.js implementation of garbled conversion is the AJAX part, and this part of the present I am too relevant

Then the exchange of replies, did not get a reply, so this method, at present, is not available.

Then a netizen corrects my concept,/u4e2d/u56fd such content is not garbled but transcoding, so there is no reason to use iconv, and I was this meaning (expressed as garbled is because the article was read before), according to my understanding of the ICONV function is to implement transcoding, He said that this is the Unicode value of Chinese characters, and then Baidu Google a bit, find a lot of related articles, is looking at, get replies, after the replies to give some written code,

Script type= "Text/javascript" src= "jquery-1.4.2.min.js" ></script> <mce:script type= "Text/javascript" ><!--$ (function () {$.get ("http://int.dpool.sina.com.cn/iplookup/iplookup.php", "Format:js", function (e) {$ (" #data "). HTML (e);}); }); --></mce:script> <div id= "Data" ></div>

A new idea: using jquery as a pure front-end, I don't know about jquery, so I can't delve into it, but the code can get the desired results under IE8, as shown below:

1 60.7.0.0 60.7.255.255 China Hebei Qinhuangdao unicom

The following code is also available:

Just show words:

<?php @header ("content-type:text/html; charset:gb2312 "); function Getipplace () {$ip = file_get_contents ("Http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"); $ip = Str_replace (' var remote_ip_info ', ' var arr ', $IP); Just show the words, this is enough, that one big lump is processing the related logic return $IP; }?> <mce:script type= "Text/javascript" ><!--<?php Echo getipplace ();?>; document.write (arr[' country ')); --></mce:script>

This code performs the display as follows: China, normal display

Two more times to deal with:

<?php @header ("content-type:text/html; charset:gb2312 "); function Getipplace () {$ip = file_get_contents ("Http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"); If you have to deal with it two times then $ip = Preg_replace ('/[^/{]+/{[^;] +);/I ', '//1 ', $ip); $data = Array (); foreach (Json_decode ($ip, True) as $k => $v) {if ($k = = ' Country ') {$v = Iconv (' gb2312 ', ' utf-8 ', ' You are in '). $v; $data [$k] = $v; $ip = ' var arr = '. Json_encode ($data); return $IP; }?> <mce:script type= "Text/javascript" ><!--<?php Echo getipplace ();?>; document.write (arr[' country ')); --></mce:script>

The result is: You are in China, showing normal.

There are two more pieces of code:

<mce:script src= "Http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js" mce_src= "http:// Int.dpool.sina.com.cn/iplookup/iplookup.php?format=js "></mce:script> <mce:script type=" text/ JavaScript "><!--alert (remote_ip_info.country); --></mce:script>

Pop-up dialog box, "China", showing normal

There is another way, the following code:

<?php $s = file_get_contents ("Http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"); Preg_match ('/{.+}/', $s, $r); $p = Json_decode ($r [0]); Print_r ($p);?>

The following code appears: StdClass Object ([ret] => 1 [start] => 60.7.0.0 [end] => 60.7.255.255 [country] => China [province] => Hebei [City] => Qinhuangdao [District] => [ISP] => unicom [type] => [desc] =>)

The following is my analysis:

In fact, at first my design thought is wrong, var remote_ip_info = {"ret": 1, "Start": "60.7.0.0", "End": "60.7.255.255", "Country": "/U4E2D/U56FD", " Province ":"/u6cb3/u5317 "," City ":"/u79e6/u7687/u5c9b "," District ":" "," ISP ":"/u8054/u901a "," type ":" "," desc ":" "}; itself content is JS form (this point from the URL inside Format=js can see), the third and fifth code is easy to draw

Next I have to look at the regular expression section

Related Article

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.