您好,欢迎访问一九零五行业门户网

PHP中实现获取IP和地理位置类分享,ip地理位置_PHP教程

php中实现获取ip和地理位置类分享,ip地理位置发现之前有一个脚本没有写完,简单扩充了一下。
获取ip使用的是经典的逻辑,地理定位使用的是sina的通用接口。
使用方法详见源码:
复制代码 代码如下:
/*
 * get location by sina ip interface
 *
 *              @soulteary 2013.04.14
 */
classip
{
    private $args=array();
function__construct()
    {
        $this->args=self::init_args(func_get_args());
        $ip=self::getip();
$ret=preg_match_all('/(\d+\.){3}\d+/i',$ip,$result);
        if(!$ret){
            returnfalse;
        }else{
            $result=$result[0];
        }
if(isset($this->args['onlyip'])&&$this->args['onlyip']==true){
if(isset($this->args['format'])&&$this->args['format']=='json'){
                $result=json_encode($result);
            }else{
                $result=implode(',',$result);
            }
            if(isset($this->args['echo'])&&$this->args['echo']==true){
                echo$result;
                returntrue;
            }else{
                return$result;
            }
        }else{
$apiurl='http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$result[0];
            if(isset($this->args['format'])&&$this->args['format']=='json'){
                $apiurl.='&format=json';
                $return=$this->ipcurl($apiurl);
            }else{
                $return=$this->ipcurl($apiurl);
                $return=iconv(gbk//ignore,utf-8,$return);
            }
if(isset($this->args['echo'])&&$this->args['echo']==true){
                echo$return;
                returntrue;
            }else{
                return$return;
            }
}
}
public functioninit_args($args)
    {
        $result=array();
        for($i=0,$n=count($args);$i            $result=self::associative_push($args[$i],$result);
        }
        return$result;
    }
public functionassociative_push($arr,$tmp)
    {
        if(is_array($tmp)){
            foreach($tmpas$key=>$value){
                $arr[$key]=$value;
            }
            return$arr;
        }
        returnfalse;
    }
public functiongetip()
    {
        if(isset($_server['http_x_forwarded_for'])&&$_server['http_x_forwarded_for']&&strcasecmp($_server['http_x_forwarded_for'],'unknown')){
            return$_server['http_x_forwarded_for'];
        }elseif(isset($_server['remote_addr'])&&$_server['remote_addr']&&strcasecmp($_server['remote_addr'],'unknown')){
            return$_server['remote_addr'];
        }
    }
private functionipcurl($url)
    {
        $ch=curl_init();
        curl_setopt($ch,curlopt_url,$url);
        curl_setopt($ch,curlopt_header,0);
        ob_start();
        curl_exec($ch);
        curl_close($ch);
        $result=ob_get_contents();
        ob_end_clean();
        return$result;
    }
}
?>
demo
code:
getip
'onlyip'=>true,'echo'=>true
true,'echo'=>true));?>
'onlyip'=>true,'format'=>'json','echo'=>true
true,'format'=>'json','echo'=>true));?>
get location
'echo'=>true
true));?>
'format'=>'json','echo'=>true
'json','echo'=>true));?>
http://www.bkjia.com/phpjc/955974.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/955974.htmltecharticlephp中实现获取ip和地理位置类分享,ip地理位置 发现之前有一个脚本没有写完,简单扩充了一下。 获取ip使用的是经典的逻辑,地理定位使...
其它类似信息

推荐信息