
:: Anbieterverzeichnis ::
Globale Branchen
Informieren Sie sich über ausgewählte
Unternehmen im
Anbieterverzeichnis von SELFPHP
:: SELFPHP Forum ::
Fragen rund um die Themen PHP? In über
120.000 Beiträgen finden Sie sicher die passende
Antwort! 
:: Newsletter ::
Abonnieren Sie hier den kostenlosen
SELFPHP Newsletter!
:: Qozido ::
Die Bilderverwaltung mit Logbuch für
Taucher und Schnorchler. 
|
|
Geo-Daten mit MaxMind zu einer IP-Adresse ermitteln  |
Beispielaufgabe
Mit der nachfolgenden PHP5 Klasse stellen wir Ihnen eine Möglichkeit vor, mit der Sie komfortabel weitere Informationen wie z.B. Ort, Latitude, Longitude, ISP oder die Organization basierend von einer gegebenen IP-Adresse ermitteln können. Diese Informationen können hilfreich sein, um zu erfahren woher ein Besucher auf der Webseite kommt. Dabei greift die Klasse auf einen bestehenden Service der Firma MaxMind (www.maxmind.com) zurück, die diese Informationen einmal in einer kostenlosen Variante und einer kostenpflichtigen Variante anbietet. Der Unterschied ist lediglich die Genauigkeit der Daten. Im Nachfolgenden sehen Sie den Zugriff auf die Klasse.
Beschreibung
Bitte achten Sie darauf, dass für den MaxMind-Service eine Registrierung notwendig ist. Im Verlauf der Registrierung erhalten Sie dann Ihre Lizenznummer, der für den Service von Maxmind notwendig ist und den Sie in der Klasse ebenfalls eintragen müssen.
http://www.maxmind.com/app/demo_request?type=city_ws
Die Klasse wurde unter der BSD License gestellt und kann somit für private und kommerzielle Zwecke genutzt werden.
<?PHP
/**
*
* This is maxMindWebservice. Managed MaxMind Web Services GeoIP.
*
*
* @package maxMindWebservice
* @author Damir Enseleit
* @copyright 2010, SELFPHP OHG
* @license BSD License
* @version 1.0.0
* @link http://www.selfphp.de
*
*/
class maxMindWebservice
{
private $MaxMindlicense = ""; // License for MaxMind Web Services
private $MaxMindurl = ""; // MaxMind Web Services URL
private $MaxMindError = ""; // MaxMind Response-Error
private $geoIP = ""; // IP for Geo-Tracking
private $emptyVars = true; // If empty vars - don't go to Maxmind
private $geoData = array (); // Store the Geo Data for the IP
// Store all Warnings from maxmind-warnings.txt
private $MaxMindWarning = array ( 'IP_NOT_FOUND','COUNTRY_NOT_FOUND',
'CITY_NOT_FOUND','CITY_REQUIRED','POSTAL_CODE_REQUIRED','POSTAL_CODE_NOT_FOUND' );
// Store all Fatal-Errors from maxmind-fatalerrors.txt
private $MaxMindFatalErrors = array ( 'INVALID_LICENSE_KEY',
'MAX_REQUESTS_PER_LICENSE', 'IP_REQUIRED','LICENSE_REQUIRED',
'COUNTRY_REQUIRED','MAX_REQUESTS_REACHED' );
// Error Levels
private $status = array ( 'OK', 'ERROR', 'WARNINGS', 'FATALERRORS');
private $statusLevel = 'OK'; // Actual Status-Error-Level
/*
* Constructor
*/
public function __construct( $license = null, $url = null ) {
$this->MaxMindlicense = $license;
$this->MaxMindurl = $url;
if ( $license != null AND $url != null ) {
$this->emptyVars = false;
}
}
/**
* Get the Error Level from MaxMind
*
* @return string An string with the Errod-Level.
*
*/
public function getMaxMindErrorStatus () {
return $this->MaxMindError;
}
/**
* Get the Error Level
*
* @return string An string with the Errod-Level.
*
*/
public function getErrorStatus () {
return $this->statusLevel;
}
/**
* Get the Geo data in a array
*
* @return array An array with all Geo data.
*
*/
public function getGeoData ( ) {
return $this->geoData;
}
/**
* Start GeoIP with the given URL
*
* @param string $ip The IP for Geo tracking
*
* @return bool True or False
*
*/
public function startGeoIP ( $ip = null ) {
if ( $ip != null AND $this->emptyVars == false ) {
$this->geoIP = $ip;
$this->geoData = array ();
// Set Errors to null
$this->statusLevel = $this->status[0];
$this->MaxMindError = '';
if ( $this->trackingGeoIP () == true ){
return true;
}
else {
return false;
}
}
else {
return false;
}
}
/**
* Begin the Tracking with an given IP to MaxMind
*
* @return bool True or False
*
*/
private function trackingGeoIP () {
$query = $this->MaxMindurl . "?l=" . $this->MaxMindlicense . "&i=" . $this->geoIP;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout)
or die('Can not open connection to MaxMind-Server.');
if ($fp) {
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = split("\n", $buf);
$data = $lines[count($lines)-1];
fclose($fp);
if ( $this->scanMaxMindErrors( $data ) == true ) {
$geo = explode(",",$data);
$this->geoData = $geo;
return true;
}
else {
return false;
}
}
else {
# enter error handling code here
return false;
}
}
private function scanMaxMindErrors ( $data = null) {
// Scan warnings
for ( $i = 0; $i < count($this->MaxMindWarning); $i++ ) {
if (eregi($this->MaxMindWarning[$i], $data)) {
$this->MaxMindError = $this->MaxMindWarning[$i];
$this->statusLevel = $this->status[2];
return false;
}
}
// Scan Fatal-Errors
for ( $i = 0; $i < count($this->MaxMindFatalErrors); $i++ ) {
if (eregi($this->MaxMindFatalErrors[$i], $data)) {
$this->MaxMindError = $this->MaxMindFatalErrors[$i];
$this->statusLevel = $this->status[3];
return false;
}
}
return true;
}
}
?>
|
Anwendungsbeispiel
<?PHP
include ("maxmind-webservice.inc.php");
$maxmind = new maxMindWebservice("Ihre Lizenz","http://geoip1.maxmind.com/f");
$maxmind->startGeoIP("80.144.87.13");
$data = $maxmind->getGeoData();
print_r( $data );
?>
|
Ausgabebeispiel: Browseransicht
Array
(
[0] => DE
[1] => 07
[2] => Witten
[3] =>
[4] => 51.433300
[5] => 7.333300
[6] => 0
[7] => 0
[8] => "Deutsche Telekom AG"
[9] => "Deutsche Telekom AG"
)
|

|
|
|
|
|


|