新浪的接口地址 http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=202.198.1.156
以下逐次查询新浪并保存在本地数据库中.
/**
* 从新浪获取IP地址库*/
public function ips() {
set_time_limit(0);
$t = table('ice_ips');
//取最后一个地址
$ip_to=$t->get('ip_to',null,'id desc');
//$ip_to='0.255.255.255';
$ip=$this->ipsIncrease($ip_to);
if(!$ip){
echo 'over';
return;
}
//递增查询
while ( true ) {
$ipStr = implode('.', $ip);
echo "\r\n".$ipStr."\t";
while(true){
$ret = file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ipStr);
if($ret){
break;
}
}
$mid = LCatcher::mid($ret, '=', ';');
$r = json_decode($mid);
writeLog('ips', array($ret,$mid,$r));
$t->insert(array(
'ip_from' => $r->start,
'ip_to' => $r->end,
'country' => $r->country,
'province' => $r->province,
'city' => $r->city,
'district' => $r->district,
'isp' => $r->isp,
'type' => $r->type,
'desc' => $r->desc
));
$to = $r->end;
$ip=$this->ipsIncrease($to);
if(!$ip){
echo 'over';
return;
}
}
}
/**
* 将IP地址加1,返回数组格式
* @param string $str
* @return multitype:|number|boolean
*/
private function ipsIncrease($str) {
$ip = explode('.', $str);
$ip [3] ++;
if ($ip [3] <= 255) {
return $ip;
}
$ip [3] = 0;
$ip [2] ++;
if ($ip [2] <= 255) {
return $ip;
}
$ip [2] = 0;
$ip [1] ++;
if ($ip [1] <= 255) {
return $ip;
}
$ip [1] = 0;
$ip [0] ++;
if($ip[0]==10){
$ip[0]==11;
}
if ($ip [0] <= 255) {
return $ip;
}
//如果已经到达255.255.255.255,返回失败
return false;
}