Solved

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

Posted on 2011-02-21
8
350 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I dont know why my code is no longer working help please 9 62
Ajax and PHP 4 31
AJAX Wordpress Not Reading Variable 2 31
Need a modeling tool 2 19
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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 look for a specific file type in a local or remote server directory using PHP.

820 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