Link to home
Start Free TrialLog in
Avatar of Valleriani
VallerianiFlag for Sweden

asked on

PHP - SimpleXML .. A way to cycle through all information in receieved XML data

I've attached the code on what I have so far.

Pretty much what I do is get the data sent back to me in XML and parse it using SIMPLEXML. What I want to grab is a few specific variables, anything that matches, [name], [companyName], [streetAddress]. etc.. The thing is, the data recieved is always different, so sometimes there more then one streetAddress, companyName, etc.. And I need to grab them all.

What would be the best way to grab ALL the required data fields regardless on how many records there are.. the goal is to add it all to a text file so it can be used as a blacklist on our end (aka blacklisted domains/companies/etc for our servers.)

Thanks!
if (!empty($_POST)) {
	$domain = $_POST['domain']; 
	$arecord = gethostbyname($domain);
	$url = 'http://whois.arin.net/rest/ip/' . $arecord . '/pft';
	$data = file_get_contents($url);
	
	$obj = SimpleXML_Load_String($data);
	print_r($obj);	
}
?>

<html><body>
<h4>Whois Blacklist Checker</h4>
<form action="index.php" method="post"> 
Enter Domain: <input name="domain" type="text" /> 
<input type="submit" />
</form>
</body></html>

Open in new window

Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Regardless of how much the data varies, XML always has a structure. If there are going to be varying amounts of data then you should know where they are going to vary. For instance in the example you give above where there is more than one address I would expect to see something like

<record>
   <address>
     ....data etc...
   </address>
</record>

and for repeats

<record>
   <address>
     ....data etc...
   </address>
   <address>
     ....data etc...
   </address>
</record>


so for this I would do something like

foreach ($obj->address as $anAddress ) {
    .... process one address group
}

With seeing the XML it's hard to be more specific.
SOLUTION
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland 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
bportlock - copy the code and submit any web domain - there you can see the XML
@Roads - I must be in dopey mode this morning I tried http://whois.arin.net/rest/ip/yahoo.com/pft and got Your request could not be processed because it is not composed correctly.

:-(
Try through the code snippet form - it returns XML.
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
Avatar of Valleriani

ASKER

For this I believe I am going to be able to use both..

// for name array
foreach($obj->children() as $value)
array_push($mynemearray,$value);

So that POC, ORG, NET, etc are the same, then I can use your snippet to process through the array. I believe that should work but need to check it out in a few.

Thanks both so far!
Both are very helpful and I used a mix of both to complete this issue. Thank you!