Link to home
Start Free TrialLog in
Avatar of ironpen45
ironpen45

asked on

parsing xml data from ipinfodb script

ipinfodb offers php examples of querying their servers for ip address data. i'm trying to adapt it to extract the following info:

statusCode : OK
statusMessage :
ipAddress : 66.39.122.10
countryCode : US
countryName : UNITED STATES
regionName : NEW YORK
cityName : BELLEVILLE
zipCode : 10109
latitude : 40.7949
longitude : -74.1623
timeZone : -05:00

here's the code
<?php

    include "config.php";
	
	$to 		= "tracker@company.com";
	$sFrom		= "tracker@company.com";
	$header 	= "From: Visit Tracker<" . $sFrom . ">\r\n";
	$pageviewed	= $_REQUEST['pageviewed'];
	$browser =  $_SERVER['HTTP_USER_AGENT'];
	$sIp	= $_SERVER ['REMOTE_ADDR'];

	protected $errors = array();
	protected $service = 'api.ipinfodb.com';
	protected $version = 'v3';
	protected $apiKey = 'da7f93275343d8ddb7e12d1ca39c87721c07cbc251d9cec3be0e508ecbd61569';

	public function __construct(){}

	public function __destruct(){}

	public function setKey($key){
		if(!empty($key)) $this->apiKey = $key;
	}

	public function getError(){
		return implode("\n", $this->errors);
	}

	public function getCountry($sIp){
		return $this->getResult($sIp, 'ip-country');
	}

	public function getCity($sIp){
		return $this->getResult($sIp, 'ip-city');
	}

	private function getResult($sIp, $name){
		$ip = @gethostbyname($sIp);

		if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
			$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');

			try{
				$response = @new SimpleXMLElement($xml);

				foreach($response as $field=>$value){
					$result[(string)$field] = (string)$value;
				}

				return $result;
			}
			catch(Exception $e){
				$this->errors[] = $e->getMessage();
				return;
			}
		}

		$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
		return;
	}

	//Get errors and locations
	$country_code 	= $response->countryCode;
	$country_name 	= $response->countryName;
	$region_name 	= $response->regionName;
	$city 			= $response->cityName;
	$zippostalcode 	= $response->zipCode;
 
	$sQuery = "SELECT IDREC FROM tblVisits ORDER BY IDREC DESC LIMIT 1";
	$rResult = mysql_query ($sQuery, $link);

	$row = mysql_fetch_assoc($rResult);

	$IDREC  = 1;

	if (!empty($row)) {
		$IDREC = (int)$row['IDREC'] + 1;
	}

	$sQuery = "INSERT INTO tblVisits SET IDREC=" . $IDREC . ", IPADDRESS='" . $sIp . "', PAGEVIEWED='" . $pageviewed . "', DATETIME='" . date(DATE_RFC822) . "', COUNTRY='" . $country_name . "', REGION='" . $region_name . "', CITY='" . $city . "', ZIP='" . $zippostalcode . "', BROWSER='" . $browser . "'";
	mysql_query ($sQuery);

	$subject	= "Visit from : " . $city . ", " . $region_name . ", " . $country_name . " on page: " . $pageviewed;

 	$body 		= "Page Viewed  : " . $pageviewed . "\nIP Address     : " . $sIp . "\nDate         : " . date(DATE_RFC822) . "\n\nCountry          : " . $country_name . "\nRegion\State : " . $region_name . "\nCity         : " . $city . "\nZip          : " . $zippostalcode . "\n\nMap          :  http://ip-whois-lookup.com/lookup.php?ip=" . $sIp . "\n\nBrowser : " . $browser;

//	$body		= " . str_pad("Page Viewed:", 10, STR_PAD_LEFT) . $pagegviewed . str_pad("\nIP Address:", 10, STR_PAD_LEFT) . $sIp . str_pad("\nDate:",10, STR_PAD_LEFT) . date(DATE_RFC822) . str_pad("\nCountry:", 10, STR_PAD_LEFT) . $country_name . str_pad("\nRegion \ State:", 10, STR_PAD_LEFT) . $region_name . str_pad("\nRegion \ City:", 10, STR_PAD_LEFT) . $city . str_pad("\nZIP:", 10, STR_PAD_LEFT) . $zippostalcode . str_pad("\nMap:", 10, STR_PAD_LEFT) . "http://ip-whois-lookup.com/lookup.php?ip=" . $sIp . str_pad("\nBrowser :", 10, STR_PAD_LEFT) . $browser";
	
	mail ($to, $subject, $body, $header);
		
	echo("true;");

?>

Open in new window


I can't determine where's the problem. Please help
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ironpen45
ironpen45

ASKER

thanks ray

the final xml output displayed is

<p>
<strong>First result</strong><br />
statusCode : OK<br />
statusMessage : <br />
ipAddress : 68.39.122.10<br />
countryCode : US<br />
countryName : UNITED STATES<br />
regionName : NEW JERSEY<br />
cityName : BELLEVILLE<br />

zipCode : 10109<br />
latitude : 40.7949<br />
longitude : -74.1623<br />
timeZone : -05:00<br />
</p>
<p>
<strong>Dump of all errors</strong><br />
No errors<br />
</p>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had to be away today so I couldn't follow up.  Glad you found a good solution.  All the best, ~Ray
Thanks Ray. I used GEO I/O whose API is similar to the old IPINFODB. The output is just plain delmited text, etc.