How to Fetch FeedBurner feed with PHP and CURL

The code below fetch the feed contents using PHP and CURL request using SimpleXML.

<?php
// URL location of your feed
$feedUrl = "http://feeds.feedburner.com/kapsblog?format=xml";
$feedContent = "";

// Fetch feed from URL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $feedUrl);
curl_setopt($curl, CURLOPT_TIMEOUT, 3);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);

// FeedBurner requires a proper USER-AGENT...
curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");

$feedContent = curl_exec($curl);
curl_close($curl);

// check if content received
if($feedContent && !empty($feedContent)) {
	$feedXml = @simplexml_load_string($feedContent);
	if($feedXml) {
?>
	<h2>From The Blog...</h2>
	<ul>
	  <?php foreach($feedXml->channel->item as $item) { ?>
		<li style="padding:4px 0;"><a href="<?php echo $item->link; ?>"><?php echo $item->title; ?></a></li>
	  <?php } // end foreach ?>
	</ul>
<?php
	} // end if
} // end if
?>


Leave a Reply

You must be logged in to post a comment.