ip地理位置查找

function postionFind($ip)
    {
        $ch = curl_init();
        $url = "http://www.ip138.com/ips.asp?ip=$ip&action=2";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);
        $output = mb_convert_encoding(curl_exec($ch), 'UTF-8', 'GBK');
        curl_close($ch);
        if (preg_match('/<li>本站主数据:(.+)<\/li>/Ui', $output, $m))
        {
            return $m[1];
        }
    }


$ip = !empty($_SERVER["HTTP_X_FORWARDED_FOR"])

?$_SERVER["HTTP_X_FORWARDED_FOR"]

:$_SERVER["REMOTE_ADDR"];

$position = postionFind($ip);

你可能感兴趣的:(server,function,url,encoding,output)