Search code examples
phpsimplexml

PHP SimpleXMLElement - Can't Retrieve 'Description' Element From Feed


I don't have much idea of crawling XML documents. Might be a little mistake.

Feed URL: http://www.simplifyingthemarket.com/feed/

Code:-

$url = 'http://www.simplifyingthemarket.com/feed/';
$XmlObject = new SimpleXmlElement( file_get_contents($url) );
print_r($XmlObject->channel[0]->item[0]->description[0]);

Output:-

SimpleXMLElement Object ( )

Required Output:-

<a href="http://www.simplifyingthemarket.com/wp-content/uploads/2015/04/Where-Should-I-Retire.jpg"><img class="alignnone wp-image-30608" src="http://www.simplifyingthemarket.com/wp-content/uploads/2015/04/Where-Should-I-Retire.jpg" alt="Where Should I Retire [INFOGRAPHIC] &#124; Simplifying The Market" width="600" height="1035" /></a>
 <h4>Some Highlights From The Report:</h4>
 <ul>
 <li>80% of all pre-retirees in the South Atlantic region plan to stay there in retirement</li>
 <li>4 out of 10 pre-retirees plan to relocate in retirement</li>
 <li>Retirees in the South Central Region are most satisfied with their Cost of Living</li>
 <li>For more information or to read the full report: <a href="https://www.ml.com/articles/age-wave-survey.html">Click Here</a></li>
 </ul>

Note: My client has purchased their feed, so there is no legal issues. More information can be provided on contact.


Solution

  • In order to access the node's value as a string you first have to cast it to a string. In your case it would be like:

    print_r((string) $XmlObject->channel[0]->item[0]->description[0]);
    

    Credits: The SimpleXMLElement class