27. Januar 2010: GeoIP-Lookup in GeoRSS umwandeln
Um die Frage zu beantworten wie ich denn IP-Adressen in eine Google-Karte bekommen habe: Mittels geoip, GeoRSS und (logisch) Google-Maps. Und zwar folgendermaßen.
geoiplookup <IP> >> geolookup.txtspeichert die Ausgabe in einer Datei.- Mit regulären Ausdrücken extrahiert man die Längen- und Breitenangaben.
- Diese legt man als GeoRSS-Feed ab.
- Den Feed wiederum kann man in Google-Maps importieren.
Fertig ist die Karte. Wer es ein wenig einfacher haben will, kann auch folgendes PHP-Script zum umwandeln der geoip-Ausgabe in einen GeoRSS-Feed nutzen.
PHP:
<?php
// Check for Data
if(!$_POST['geoiplookup']){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<textarea rows="20" cols="50" name="geoiplookup"></textarea><br />
<input type="submit" />
</form>
</body>
</html>
<?php
die();
}
// Setup Variables
$regex = "/^.+Rev 1: ([^,]+), ([^,]+), ([^,]+), ([^,]+), ([0-9\.\-]+), ([0-9\.\-]+), [^,]+, [^,]+$/";
$geodata['line'] = "<entry><title>%s, %s, %s</title><georss:point>%s %s</georss:point></entry>\n";
$geodata['points'] = array();
$geodata['rawdata'] = explode("\n", $_POST['geoiplookup']);
// XML-Special Characters-Function (alonso05 at gmail dot com)
function xml_character_encode($string, $trans=''){
$trans = (is_array($trans)) ? $trans : get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($trans as $k=>$v)
$trans[$k]= "&#".ord($k).";";
return strtr($string, $trans);
}
// Parse input
foreach($geodata['rawdata'] as $data)
if(preg_match_all($regex, $data, $results))
$geodata['points'][] = array(
xml_character_encode(htmlentities($results[3][0])),
xml_character_encode(htmlentities($results[2][0])),
xml_character_encode(htmlentities($results[1][0])),
$results[5][0],
$results[6][0]
);
// Create GeoRSS-Feed
header('Content-type: application/xml');
echo('<?xml version="1.0" encoding="utf-8"?>');
echo('<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">');
foreach($geodata['points'] as $point)
vprintf($geodata['line'], $point);
echo('</feed>');
?>
// Check for Data
if(!$_POST['geoiplookup']){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<textarea rows="20" cols="50" name="geoiplookup"></textarea><br />
<input type="submit" />
</form>
</body>
</html>
<?php
die();
}
// Setup Variables
$regex = "/^.+Rev 1: ([^,]+), ([^,]+), ([^,]+), ([^,]+), ([0-9\.\-]+), ([0-9\.\-]+), [^,]+, [^,]+$/";
$geodata['line'] = "<entry><title>%s, %s, %s</title><georss:point>%s %s</georss:point></entry>\n";
$geodata['points'] = array();
$geodata['rawdata'] = explode("\n", $_POST['geoiplookup']);
// XML-Special Characters-Function (alonso05 at gmail dot com)
function xml_character_encode($string, $trans=''){
$trans = (is_array($trans)) ? $trans : get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($trans as $k=>$v)
$trans[$k]= "&#".ord($k).";";
return strtr($string, $trans);
}
// Parse input
foreach($geodata['rawdata'] as $data)
if(preg_match_all($regex, $data, $results))
$geodata['points'][] = array(
xml_character_encode(htmlentities($results[3][0])),
xml_character_encode(htmlentities($results[2][0])),
xml_character_encode(htmlentities($results[1][0])),
$results[5][0],
$results[6][0]
);
// Create GeoRSS-Feed
header('Content-type: application/xml');
echo('<?xml version="1.0" encoding="utf-8"?>');
echo('<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">');
foreach($geodata['points'] as $point)
vprintf($geodata['line'], $point);
echo('</feed>');
?>
Kommentieren