Curl xml reader (shows 2 items with max 100 characters)


<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://rss.cnn.com/rss/money_news_international.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch); curl_close($ch);
$xml = new SimpleXMLElement($output);
$count = 0;
$limit = 2;  

foreach( $xml->channel->item as $item ) {
    echo '<a target="_blank" href="'.$item->guid.'">'.$item->title.'</a>';
    echo ''.(substr($item->description, 0, 100)).'...';
    $count++;

    if($count == $limit) {
        break;
    }

}
?>
Author: michael schouman on September 23, 2011
Category: Code snippets

Leave a Reply

Last articles