Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-21
8
Medium Priority
?
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

730 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