...

View Full Version : Using PHP to post RSS Content?



javanewbie7
07-13-2012, 11:54 PM
OK so I'm trying to post some news headlines on a site from this rss feed http://feeds.feedburner.com/NewshourHeadlines?format=xml. I'm really new at this and aren't having any luck. So I found a tutorial that used the below code. I tested it as is and it didn't work. What am I doing wrong? IS there any additional sites that goes into great detail on how to work with rss feeds?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
</head>
<body>

<?php

$doc = new DOMDocument();
$doc->load('http://www.softarea51.com/rss/windows/Web_Development/XML_CSS_Utilities.xml');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}

?>

<?php

echo $arrFeeds();

?>

</body>
</html>

Fou-Lu
07-13-2012, 11:59 PM
You can't use echo $arrFeeds(). Array() is a construct, so if you attempted to call it as a variable function its guaranteed to fail.

So what you have here is a multidimensional array for $arrFeeds. You need to iterate it:


foreach ($arrFeeds AS $item)
{
printf('Title: %s' . PHP_EOL, $item['title']);
}

For example. Or you can skip the $arrFeeds completely and print from within the foreach on the domdocument.

mlseim
07-14-2012, 03:57 AM
Here's a way ... with comments

Try this:



<?php
// rss page for Softarea51 -
$feed_url = "http://www.softarea51.com/rss/windows/Web_Development/XML_CSS_Utilities.xml";

# INITIATE CURL.
$curl = curl_init();

# CURL SETTINGS.
curl_setopt($curl, CURLOPT_URL,"$feed_url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);

# GRAB THE XML FILE.
$xmlFile = curl_exec($curl);

curl_close($curl);

# SET UP XML OBJECT.
# Use either one of these, depending on your PHP features.
# Comment-out the line you are not using.
//$xml = new SimpleXMLElement($xmlFile);
$xml = simplexml_load_string($xmlFile);

// How many items to display
$count = 10;

// How many characters from each item
// 0 (zero) will show them all.
$char = 200;

foreach ($xml->channel->item as $item) {
if($char == 0){
$newstring = $item->description;
}
else{
$newstring = substr($item->description, 0, $char);
}
if($count > 0){
//in case they have non-closed italics or bold, etc ...
echo"</i></b></u></a>\n";
echo"
<div style='font-family:arial; font-size:.8em;'>
<b>{$item->title}</b><br />
$newstring ... <a href='{$item->guid}'>read more</a>
<br /><br />
</div>
";
}
$count--;
}
?>




.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum