Solved

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

Posted on 2011-02-21
8
347 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Valleriani
  • 3
  • 3
  • 2
8 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34941670
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
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 250 total points
ID: 34941709
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34941718
bportlock - copy the code and submit any web domain - there you can see the XML
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34941803
@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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34941838
Try through the code snippet form - it returns XML.
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 34941906
@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
 
LVL 7

Author Comment

by:Valleriani
ID: 34941956
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
 
LVL 7

Author Closing Comment

by:Valleriani
ID: 34942690
Both are very helpful and I used a mix of both to complete this issue. Thank you!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now