在項目中用到查詢IP來源地址的功能,在網上搜索了一下免費的IP地址數據庫未果,就直接使用ip138網站做爲數據來源了,通過直接使用PHP代碼去查詢IP信息,然後分析網頁來得到地址,最後插入數據庫。
$content = iconv("gb2312", "utf-8",file_get_contents("http://www.ip138.com/ips8.asp?action=2&ip=$ip"));
var_dump($content);
$pos = strpos($content, "<li>本站主數據");
echo "起始位置$pos</br>";
if(!$pos){
hi_logerr("沒有查詢到IP所在地地址!");
echo "error</br>";
return false;
}
$endPos = stripos($content, "</li>", $pos);
$jumpLen = strlen("<li>本站主數據:");
$address = substr($content, $pos + $jumpLen, $endPos - $pos - $jumpLen);
$sql = "INSERT INTO `ipmap` (`ip` ,`from` ,`updateTime` )VALUES ('$c_ip','$address',CURRENT_TIMESTAMP )";