Scripts

Class: HostIP

Hier op PHPHulp zijn al regelmatig discussies gevoerd over het koppelen van een IP adres aan een land of plaats. Hoewel dit zo goed als onmogelijk is, zijn er een aantal technieken voor ( http://www.private.org.il/IP2geo.html ). Er zijn een aantal diensten die redelijk nauwkeurig de plaats van het IP adres kunnen bepalen, helaas moet hier bijna altijd voor betaald worden. Ik kwam het volgende GPL project tegen: http://www.hostip.info/ hoewel dit nog minder betrouwbaar is dan de meeste betaalde diensten (in mijn geval wordt er ook geen plaats gevonden), is het misschien toch leuk om eens te gebruiken. Daarom heb ik een klasse geschreven die het makkelijker moet maken deze gegevens op te vragen. Voorbeelden van mogelijke aanroepen zijn hier te vinden: http://php.n3rd.nl/clsHostIP/ Het land waar jij vandaan komt: (volgens hostip.info tenminste ;) )

class-hostip
<?php

/**
 * Beplaal de locatie van een IP
 * Met behulp van het GPL project: hostip.info
 * 
 * @author Boaz den Besten
 * @version 0.1.27-07-2007
 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
 * @PHPversion 5.2.3-dev
 */
class clsHostIP{
	
	/**
	 * IP om gegevens te koppelen
	 *
	 * @var String IP Adres
	 */
	private $m_sIPAddr = null;
	
	/**
	 * Stad wat bij het ip hoort
	 *
	 * @var String City
	 */
	private $m_sCity = null;
	
	/**
	 * Land wat bij het ip hoort
	 *
	 * @var String Land
	 */
	private $m_sCountryName = null;
	
	/**
	 * Land afkorting eg NL voor Netherlands
	 *
	 * @var String Land (afkoring)
	 */
	private $m_sCountryAbbrev = null;
	
	/**
	 * Breedtegraad die bij het IP hoort 
	 *
	 * @var Float breedtegraad
	 */
	private $m_fLatitude = null;
	
	/**
	 * Lengtegraad die bij het IP hoort 
	 *
	 * @var Float lengtegraad
	 */
	private $m_fLongitude = null;
	
	/**
	 * Een instance verkrijgen
	 * En informatie over het IP verzamelen
	 *
	 * @param String IP adres
	 */
	public function __construct($p_sIPAddr){
		if(filter_var($p_sIPAddr, FILTER_VALIDATE_IP) === false){
			throw new Exception('Not a valid IP adress.', 101);
		}
		$this->m_sIPAddr = $p_sIPAddr;
		
		$this->getIPInfo();
	}
	
	/**
	 * Method om informatie te verzamelen
	 *
	 */
	private function getIPInfo(){
		$oSimpleXML = new SimpleXMLIterator('http://api.hostip.info/?ip='.$this->m_sIPAddr, null, true);
		foreach($oSimpleXML->getDocNamespaces() as $sPrefix => $sNs){
			$oSimpleXML->registerXPathNamespace($sPrefix, $sNs);
		}
		
		$aLocation = $oSimpleXML->xpath('//gml:featureMember/*/*');

		$this->m_sCity = (string) $aLocation[0];
		$this->m_sCountryName = (string) $aLocation[1];
		$this->m_sCountryAbbrev = (string) $aLocation[2];
		
		$aCoords = $oSimpleXML->xpath('//gml:coordinates');
		@list($this->m_fLongitude, $this->m_fLatitude) = explode(',', $aCoords[0]);
	}
	
	/**
	 * Geef het IP terug
	 *
	 * @return String
	 */
	public function getIPAddr(){
		return $this->m_sIPAddr;
	}
	
	/**
	 * Geef de stad terug
	 *
	 * @return String
	 */
	public function getCity(){
		return $this->m_sCity;
	}
	
	/**
	 * Geef het land terug
	 *
	 * @return String
	 */
	public function getCountryName(){
		return $this->m_sCountryName;
	}
	
	/**
	 * Geef de land afkoring terug (eg NL voor Netherlands)
	 *
	 * @return String
	 */
	public function getCountryAbbrev(){
		return $this->m_sCountryAbbrev;
	}
	
	/**
	 * Geef de breedtegraad terug
	 *
	 * @return Float
	 */
	public function getLatitude(){
		return $this->m_fLatitude;
	}
	
	/**
	 * Geef de lengtegraad terug
	 *
	 * @return Float
	 */
	public function getLongitude(){
		return $this->m_fLongitude;
	}
	
	/**
	 * Geef een link naar een plaatje de vlag van het land
	 *
	 * @return String
	 */
	public function getFlag(){
		return 'http://api.hostip.info/flag.php?ip='.$this->m_sIPAddr;
	}
	
	/**
	 * BluePrint HostIP
	 *
	 */
	public function __toString(){		
		return print_r($this, true);
	}
	
}

?>

Reacties

0
Nog geen reacties.