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

LVL 7
VallerianiAsked:
Who is Participating?
 
Beverley PortlockConnect With a Mentor Commented:
@Roads - thanks.

Using Yahoo.com we get XML like this

SimpleXMLElement Object
(
    [net] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [termsOfUse] => https://www.arin.net/whois_tou.html
                )

            [registrationDate] => 2007-09-13T00:00:00-04:00
            [ref] => http://whois.arin.net/rest/net/NET-67-195-0-0-1
            [endAddress] => 67.195.255.255
            [handle] => NET-67-195-0-0-1
            [name] => A-YAHOO-US8
            [nameservers] => SimpleXMLElement Object
                (
                    [nameserver] => Array
                        (
                            [0] => NS2.YAHOO.COM
                            [1] => NS1.YAHOO.COM
                            [2] => NS5.YAHOO.COM
                            [3] => NS4.YAHOO.COM
                            [4] => NS3.YAHOO.COM
                        )

                )

            [netBlocks] => SimpleXMLElement Object
                (
                    [netBlock] => SimpleXMLElement Object
                        (
                            [cidrLength] => 16
                            [endAddress] => 67.195.255.255
                            [description] => Direct Allocation
                            [type] => DA
                            [startAddress] => 67.195.0.0
                        )

                )

            [pocs] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [termsOfUse] => https://www.arin.net/whois_tou.html
                        )

                    [pocLinkRef] => Array
                        (
                            [0] => http://whois.arin.net/rest/poc/NA258-ARIN
                            [1] => http://whois.arin.net/rest/poc/NETWO857-ARIN
                        )

                )

            [orgRef] => http://whois.arin.net/rest/org/YHOO
            [parentNetRef] => http://whois.arin.net/rest/net/NET-67-0-0-0-0
            [startAddress] => 67.195.0.0
            [updateDate] => 2007-12-07T00:00:00-05:00
            [version] => 4
        )

    [org] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [termsOfUse] => https://www.arin.net/whois_tou.html
                )

            [registrationDate] => 2000-10-23T00:00:00-04:00
            [ref] => http://whois.arin.net/rest/org/YHOO
            [city] => Sunnyvale
            [iso3166-1] => SimpleXMLElement Object
                (
                    [code2] => US
                    [code3] => USA
                    [name] => UNITED STATES
                    [e164] => 1
                )

            [handle] => YHOO
            [name] => Yahoo! Inc.
            [pocs] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [termsOfUse] => https://www.arin.net/whois_tou.html
                        )

                    [pocLinkRef] => Array
                        (
                            [0] => http://whois.arin.net/rest/poc/NA258-ARIN
                            [1] => http://whois.arin.net/rest/poc/NETWO857-ARIN
                            [2] => http://whois.arin.net/rest/poc/NA258-ARIN
                        )

                )

            [postalCode] => 94089
            [iso3166-2] => CA
            [streetAddress] => SimpleXMLElement Object
                (
                    [line] => 701 First Ave
                )

            [updateDate] => 2009-05-18T00:00:00-04:00
        )

    [poc] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [relatedFunction] => T
                            [relatedDescription] => Tech
                            [termsOfUse] => https://www.arin.net/whois_tou.html
                        )

                    [registrationDate] => 2001-09-07T00:00:00-04:00
                    [ref] => http://whois.arin.net/rest/poc/NA258-ARIN
                    [city] => Sunnyvale
                    [companyName] => Yahoo Inc.
                    [iso3166-1] => SimpleXMLElement Object
                        (
                            [code2] => US
                            [code3] => USA
                            [name] => UNITED STATES
                            [e164] => 1
                        )

                    [handle] => NA258-ARIN
                    [lastName] => Netblock Admin
                    [emails] => SimpleXMLElement Object
                        (
                            [email] => Array
                                (
                                    [0] => rauschen@yahoo-inc.com
                                    [1] => netblockadmin@yahoo-inc.com
                                    [2] => jluster@yahoo-inc.com
                                    [3] => abechtel@inktomi.com
                                )

                        )

                    [note] => ARIN has attempted to validate the data for this POC, but has received no response from the POC since 2010-06-17
                    [phones] => SimpleXMLElement Object
                        (
                            [phone] => SimpleXMLElement Object
                                (
                                    [number] => +1-408-349-3300
                                    [type] => SimpleXMLElement Object
                                        (
                                            [description] => Office
                                            [code] => O
                                        )

                                )

                        )

                    [postalCode] => 94089
                    [iso3166-2] => CA
                    [streetAddress] => SimpleXMLElement Object
                        (
                            [line] => 701 First Ave
                        )

                    [updateDate] => 2006-03-28T00:00:00-05:00
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [relatedFunction] => AB
                            [relatedDescription] => Abuse
                            [termsOfUse] => https://www.arin.net/whois_tou.html
                        )

                    [registrationDate] => 2005-04-28T00:00:00-04:00
                    [ref] => http://whois.arin.net/rest/poc/NETWO857-ARIN
                    [city] => Sunnyvale
                    [companyName] => Yahoo!
                    [iso3166-1] => SimpleXMLElement Object
                        (
                            [code2] => US
                            [code3] => USA
                            [name] => UNITED STATES
                            [e164] => 1
                        )

                    [handle] => NETWO857-ARIN
                    [lastName] => Network Abuse
                    [emails] => SimpleXMLElement Object
                        (
                            [email] => network-abuse@cc.yahoo-inc.com
                        )

                    [note] => ARIN has attempted to validate the data for this POC, but has received no response from the POC since 2010-06-18
                    [phones] => SimpleXMLElement Object
                        (
                            [phone] => SimpleXMLElement Object
                                (
                                    [number] => +1-408-349-3300
                                    [type] => SimpleXMLElement Object
                                        (
                                            [description] => Office
                                            [code] => O
                                        )

                                )

                        )

                    [postalCode] => 94089
                    [iso3166-2] => CA
                    [streetAddress] => SimpleXMLElement Object
                        (
                            [line] => 701 First Avenue
                        )

                    [updateDate] => 2005-04-28T00:00:00-04:00
                )

        )

)

Open in new window



Using the POC section (becuase it has multiple data we can use in our example), this code

     $obj = SimpleXML_Load_String($data);


     foreach ( $obj->poc as $poc ) {

          $city = (string) $poc->city;
          $company = (string) $poc->companyName;
          echo "<br/>$company - $city<br/>";
          foreach( $poc->emails->email as $anEmail )
               echo "&nbsp;&nbsp;- " . (string) $anEmail . "<br/>";
          
     }

Open in new window


returns this data which clearly has varying amounts of data.

Yahoo Inc. - Sunnyvale
  - abechtel@inktomi.com
  - jluster@yahoo-inc.com
  - netblockadmin@yahoo-inc.com
  - rauschen@yahoo-inc.com

Yahoo! - Sunnyvale
  - network-abuse@cc.yahoo-inc.com
0
 
Beverley PortlockCommented:
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.
0
 
Lukasz ChmielewskiConnect With a Mentor Commented:
I think you should use array for this with a pseudocode like this:

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

Open in new window

0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Lukasz ChmielewskiCommented:
bportlock - copy the code and submit any web domain - there you can see the XML
0
 
Beverley PortlockCommented:
@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.

:-(
0
 
Lukasz ChmielewskiCommented:
Try through the code snippet form - it returns XML.
0
 
VallerianiAuthor Commented:
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!
0
 
VallerianiAuthor Commented:
Both are very helpful and I used a mix of both to complete this issue. Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.