Displaying XML Content in PHP
The easiest way to deal with XML content in PHP is to use the Simple XML functions of PHP. Using these, we can turn a bunch of XML into a PHP object and loop over it.
$url = ‘http://rss1.deepakkaletha.com/feed/’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$data = simplexml_load_string($output);
echo ‘<ul>’;
foreach($data->entry as $e){
echo ‘<li><a href="’ . $e->link[0]['href'] .
‘">’.$e->title.’</a></li>’;
}
echo ‘</ul>’;
?>
The simplexml_load_string() function turns the XML document into a PHP object with arrays. How did I figure out to loop over data->entry and get the href via link[0]['href']? Simple. I did a print_r($output) and checked the source of the document by hitting Cmd + U in Firefox on my Mac. That showed me that this entry is an array. I then did a print_r($e) in the loop to see all the properties of every entry. If it is part of the @attributes array, then you need to use the [] notation.