I’m trying to retrieve the the HREF and echo it out using SimpleXML, but I keep getting the error message below.
Warning: Invalid argument supplied for foreach() in …
<?php
$url = 'file.xml';
foreach(simplexml_load_file($url)->info->content->item as $it) {
echo $it->site-page-href;
}
?>
<?xml version="1.0" encoding="utf-8"?>
<info>
<content>
<item>
<site>
<page>
<href>http://domain.com</href>
</page>
</site>
</item>
</content>
<info>
Can anyone spot the problem?
echo $it->site-page-href;should be
echo$it->site->page->href;
end tag should be
</info>->infoas youare already at the root element.
ie.
simplexml_load_file($url)->content->itemnot
simplexml_load_file($url)->info->content->itemBtw, finding all of the href nodes can be achieved more easily if you use xpath.