欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP中实现获取IP和地理位置类分享

程序员文章站 2023-02-16 22:20:47
发现之前有一个脚本没有写完,简单扩充了一下。 获取ip使用的是经典的逻辑,地理定位使用的是sina的通用接口。 使用方法详见源码: 复制代码 代码如下: <&...

发现之前有一个脚本没有写完,简单扩充了一下。
获取ip使用的是经典的逻辑,地理定位使用的是sina的通用接口。

使用方法详见源码:

复制代码 代码如下:

<?php
/*
 * 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<$n;$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;
    }
}
 
?>
<!doctype html>
<html>
<head>
    <meta charset="utf8">
    <title>demo</title>
    <script type="text/javascript">
        varip="<?php new ip(array('onlyip'=>true, 'echo'=>true));?>";
        vardata=<?php newip(array('format'=>'json','echo'=>true));?>;
        varresult='';
        for(oo indata){
            result+=oo+':'+data[oo]+"\n";
        }
        alert(result+ip);
    </script>
</head>
<body>
<h1>code:</h1>
 
<h2>getip</h2>
 
<p>'onlyip'=>true,'echo'=>true</p>
 
<p><?phpnewip(array('onlyip'=>true,'echo'=>true));?></p>
 
<p>'onlyip'=>true,'format'=>'json','echo'=>true</p>
 
<p><?phpnewip(array('onlyip'=>true,'format'=>'json','echo'=>true));?></p>
 
<h2>get location</h2>
 
<p>'echo'=>true</p>
 
<p><?phpnewip(array('echo'=>true));?></p>
 
<p>'format'=>'json','echo'=>true</p>
 
<p><?php newip(array('format'=>'json','echo'=>true));?></p>
</body>
</html>