[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

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
0
ironpen45
Asked:
ironpen45
  • 4
  • 2
3 Solutions
 
Ray PaseurCommented:
I have used http://www.geoio.com/ to get this sort of data and I have found it to be quite dependable, so if you're having trouble with this service, the folks at GEOIO may be a good alternative.  IIRC their data comes back in a handful of formats including an easy-to-use CSV string.

Please post an example of the XML string retrieved by this statement on line 41:
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');

Once we have that I can show you how I would parse it.

Some debugging ideas... Use error_reporting(E_ALL) on every script.  Remove the @ notation from function calls (the @ suppresses error messages).  Make liberal use of var_dump() statements and when there may be XML or HTML in the var_dump() output be sure to use "view source."

Standing by, ~Ray
0
 
ironpen45Author Commented:
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>
0
 
ironpen45Author Commented:
ray: i looked at GEO I/O and their API looks like the old IPINFODB API.

would you know how parse it? i'm basically trying to fit it in my older code below. thanks!
$sBackup = file_get_contents("http://api.geoio.com/q.php?key=hU9iawlvHHNNtuFJ&qt=geoip&d=semi&q=$sIp");
		
		$sAnswer = new SimpleXMLElement($sBackup);
	if (!$sBackup) return false;
	}
	else {
		$sAnswer = new SimpleXMLElement($sData);
	}
	
	$country_name 	= $sAnswer->CountryName;
	$region_name 	= $sAnswer->RegionName;
	$city 			= $sAnswer->City;
	$zippostalcode 	= $sAnswer->ZipPostalCode;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ironpen45Author Commented:
I found my own solution.

It's not an XML issue; just use file_get_contents, then explode to extract the delimited fields and so forth.

thanks for looking.
0
 
Ray PaseurCommented:
I had to be away today so I couldn't follow up.  Glad you found a good solution.  All the best, ~Ray
0
 
ironpen45Author Commented:
Thanks Ray. I used GEO I/O whose API is similar to the old IPINFODB. The output is just plain delmited text, etc.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now