php根據ip獲取地址信息

<?php 

$url = 'http://ip.taobao.com/service/getIpInfo2.php';//淘寶提供的一個ip地址庫

$data=array('ip'=>'219.144.202.12');

function curl_post($url, $header=array(), $data=array(), $referer=NULL, $timeout=5){

    if (!$header) {

      $header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

      $header[] = "Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7";

      $header[] = "Accept-Encoding: gzip,deflate";

      $header[] = "User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";

    }



    $ch = curl_init();

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_ENCODING, '');

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    if ($referer) curl_setopt($ch, CURLOPT_REFERER, $referer);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $r = curl_exec($ch);

    curl_close($ch);

    return $r;



}

$html= curl_post($url,null,$data,'http://ip.taobao.com/ipSearch.php');

$json = json_decode($html);



echo $json->data->country.$json->data->area.$json->data->region.$json->data->city;
相關文章
相關標籤/搜索