php获取当前ip地址信息


$ip = $_SERVER["REMOTE_ADDR"];
$ipaddress = send_post("http://ip.taobao.com/service/getIpInfo2.php", array('ip'=>$ip));
$shengfen = "";
$shi = "";
if (!empty($ipaddress) && trim($ipaddress) != '') {
    $json = json_decode($ipaddress, true);
    $shengfen=$json['data']['region'];
    $shi=$json['data']['city'];
}

preg_match_all("/[^市]+/u",$shi,$h);
$str=str_replace("{shiming}",$h[0][0],$contents['title']);

//获取ip地址所对应的位置信息
function send_post($url, $postdata, $refere = null, $codes='utf-8',$gzip=0) {
    if(empty($url)) return '';
    // curl模式
    if (function_exists('curl_init') && function_exists('curl_exec')) {
        $curl = curl_init(); //初始化curl
        curl_setopt($curl, CURLOPT_URL, $url); //设置访问的网站地址
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"); //模拟用户使用的浏览器
        if (empty($refere) || trim($refere) == '') 
            $refere = "http://ip.taobao.com/ipSearch.php";
        curl_setopt($curl, CURLOPT_REFERER, $refere);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);    //自动设置来路信息
        curl_setopt($curl, CURLOPT_TIMEOUT, 100);      //设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, false);         //显示返回的header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_COOKIE, 'thw=cn; l=Ak1NnV-qg/bdZXyZwEMgQdtH3WPHS4H8; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; tracknick=dupinshijie; _cc_=VT5L2FSpdA%3D%3D; tg=0; miid=1574295740019167626; t=1b016c35adb854bedcd5e9475750ac50; cna=NrmNEY4qzFQCATo9xE1oiwDb; cookie2=15512afad215513c48a1967d642788c7; v=0; _tb_token_=e8a7abbe8708; isg=AhsbLidpxbX57zpQNtX1nFCqvn9FWC6Vxtvogg1Y95ox7DvOlcC_Qjls8nMM; PHPSESSID=979bslqugd9a0t0mr7pi7qjbf0; Hm_lvt_cdca62f337ad44ec441f4f40b393c2c7=1511527200; Hm_lpvt_cdca62f337ad44ec441f4f40b393c2c7=1511527202');

        // post数据
        curl_setopt($curl, CURLOPT_POST, 1);
        // post的变量
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);

        if($gzip) curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // 关键在这里
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($curl);
        curl_close($curl);
    } else {
        $data = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$postdata['ip']);
    }
    return $data;
}

?>

你可能感兴趣的:(php)