Right, so this is my first day using XML. I’m not creating the XML, someone is sending me a URL and I need to do something with it using PHP. This is what the XML structure looks like:
<response>
<query id="1">
<results>
<item>
<id>GZ7w39jpqwo</id>
<rank>1</rank>
<explanation>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a massa lectus, sed convallis sapien. Curabitur sem mauris, ullamcorper ut. </explanation>
</item>
<item>
<id>hfMNRUAwLVM</id>
<rank>2</rank>
<explanation>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a massa lectus, sed convallis sapien. Curabitur sem mauris, ullamcorper ut. </explanation>
</item>
<item>
<id>I_cxElavpS8</id>
<rank>3</rank>
<explanation>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a massa lectus, sed convallis sapien. Curabitur sem mauris, ullamcorper ut. </explanation>
</item>
</results>
</query>
</response>
So, yeah, this is what I’ve figured out so far…
$url = "http://www.MyURL.blah";
$string = file_get_contents($url);
$xml = simplexml_load_string($string);
echo $xml->getName();
This echoes the word ‘response’. Yay, go me! So now how the hell do you get the id, rank, and explanation of each item? I only posted 3 items above. There’ll actually be about 50.
This example may help you, it used using DOMDocument.