美文网首页
JS PHP获取所在城市

JS PHP获取所在城市

作者: Yumazhiyao | 来源:发表于2017-01-05 11:07 被阅读119次

    JS

    var province = '' ;
    var city = '' ;
    jQuery.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js",function(){
          province = remote_ip_info["province"];
          city = remote_ip_info["city"];  
          alert(city)
     }) ;
    

    PHP

    //根据现有IP地址获取其地理位置(省份,城市等)的方法
    
    function GetIpLookup($ip = ''){  
      if(empty($ip)){  
        return '请输入IP地址'; 
      }  
      $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);  
      if(empty($res)){ return false; }  
      $jsonMatches = array();  
      preg_match('#\{.+?\}#', $res, $jsonMatches);  
      if(!isset($jsonMatches[0])){ return false; }  
      $json = json_decode($jsonMatches[0], true);  
      if(isset($json['ret']) && $json['ret'] == 1){  
        $json['ip'] = $ip;  
        unset($json['ret']);  
      }else{  
        return false;  
      }  
      return $json;  
    } 
    $ipInfos = GetIpLookup('123.125.114.144'); //baidu.com IP地址  //ipInfos 是一个数组
    var_dump($ipInfos);
    

    相关文章

      网友评论

          本文标题:JS PHP获取所在城市

          本文链接:https://www.haomeiwen.com/subject/jaxzvttx.html