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