调用新浪IP API 匹配GBK

WBOY
Release: 2016-06-23 13:32:02
Original
1066 people have browsed it

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 = "无法获取!";
}
return $cip;
}

function getIPLoc_sina($queryIP){ 
    $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=' . $queryIP; 
    $ch = curl_init($url);//初始化url地址 
    curl_setopt($ch, CURLOPT_ENCODING, 'utf8');//设置一个cURL传输选项 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回 
    $location = curl_exec($ch);//执行一个cURL会话 
    $location = json_decode($location);//对 JSON 格式的字符串进行编码 
    curl_close($ch);//关闭一个cURL会话 
    $loc = ""; 
    if ($location === FALSE) return "地址不正确"; 
    if (empty($location->desc)) { 
        $loc = $location->city;
    } else { $loc = $location->desc;} 
    return $loc; 

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

 通过新浪IP取得城市  

if (strpos ( $citys, $city ) !== false) 
{// 表示找到匹 ?>
       
    
  else{
  ?>
  
  }?>


$citys 是本地数据库字段内容,为BGK
而新浪取回来的城市名字是UTF8   这样就会产生错误,怎么样解决。
此代码文件为BGK,输出新浪取回来的城市是乱码。


回复讨论(解决方案)

iconv("UTF-8","GBK",$city) 
将你获取来的数据转码一下再输出即可。。另外是GBK编码吧

楼上正解!iconv ? 字符串按要求的字符编码来转换

楼上正解,iconv ? 字符串按要求的字符编码来转换

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!