Call Sina IPAPI to match GBK

Source: Internet
Author: User
Call Sina IPAPI to match GBK Function GetIP (){
If (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"]) {
$ Cip = $ _ SERVER ["HTTP_CLIENT_IP"];
}
Elseif (! Empty ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]) {
$ Cip = $ _ SERVER ["HTTP_X_FORWARDED_FOR"];
}
Elseif (! Empty ($ _ SERVER ["REMOTE_ADDR"]) {
$ Cip = $ _ SERVER ["REMOTE_ADDR"];
}
Else {
$ Cip = "cannot be obtained! ";
}
Return $ cip;
}

Function getIPLoc_sina ($ queryIP ){
$ Url = 'http: // int.dpool.sina.com.cn/iplookup/iplookup.php? Format = json & ip = '. $ queryIP;
$ Ch = curl_init ($ url); // initialize the url address
Curl_setopt ($ ch, CURLOPT_ENCODING, 'utf8'); // sets a cURL transfer option
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 10 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // get data and return
$ Location = curl_exec ($ ch); // execute a cURL session
$ Location = json_decode ($ location); // Encode strings in JSON format
Curl_close ($ ch); // closes a cURL session
$ Loc = "";
If ($ location = FALSE) return "the address is incorrect ";
If (empty ($ location-> desc )){
$ Loc = $ location-> city;
} Else {$ loc = $ location-> desc ;}
Return $ loc;
}

$ SA_IP = getip ();
$ City = getIPLoc_sina ($ SA_IP );
?>

Get City through Sina IP

If (strpos ($ citys, $ city )! = False)
{// Indicates that the horse is found?>


Else {
?>




$ Citys is the field content of the local database, which is BGK
The city name retrieved by Sina is UTF8, which leads to errors. how can this problem be solved.
This code file is BGK, and the City retrieved from Sina is garbled.


Reply to discussion (solution)

Iconv ("UTF-8", "GBK", $ city)
Transcode the obtained data before outputting it .. It's GBK encoding.

Upstairs! Iconv? String conversion according to the required character encoding

Upstairs solution, iconv? String conversion according to the required character encoding

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.