Reputation: 79
I'm a inexpert PHP user trying to generate an XML RSS Document,
Using a PHP class RSS_PHP RSS Parser
The foreach
returns 30 <items>
which is good because that's that's the number of items on the xml source, but when trying to get the value of <title>
the result it's an Array
I don't know what I'm missing or what I need to change to get the value of the <title>
from source instead of an array.
Any help will be very appreciated, thanks.
<?php
require_once 'rss_php.php';
header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML
$xml = new SimpleXMLElement('<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"></rss>');
$xml->addAttribute('version', '2.0');
$rss = new rss_php;
$rss->load('http://rss.tvguide.com/breakingnews');
$items = $rss->getItems();
$channel = $xml->addChild('channel'); //add channel node
$title = $channel->addChild('title','Channel Title Here'); //title of the feed
$link = $channel->addChild('link','http://www.zstreambox.com'); //feed site
$description = $channel->addChild('description','Channel description here'); //feed description
$language = $channel->addChild('language','en-us'); //language
$ttl = $channel->addChild('ttl','120'); //time
$image = $channel->addChild('image'); //add atom node
$url = $image->addChild('url','http://static.tvgcdn.net/www/img/tvguide-feed-logo.png'); //add channel image
$title = $image->addChild('title','ZSB: Breaking News'); //add channel title
$link = $image->addChild('link','http://www.zstreambox.com'); //add channel link
foreach($items as $index => $item) {
$item = $channel->addChild('item'); //add item node
$title = $item->addChild('title', $item['title']); //add title node
}
echo $xml->asXML();
?>
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Channel Title Here</title>
<link>http://www.zstreambox.com</link>
<description>Channel description here</description>
<language>en-us</language>
<ttl>120</ttl>
<image>...</image>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
</channel>
</rss>
The example provided by the PHP Class website is:
<?php
require_once 'rss_php.php';
$rss = new rss_php;
$rss->load('http://digg.com/rss/index.xml');
$items = $rss->getItems();
$html = '';
foreach($items as $index => $item) {
$html .= '<p><a href="'.$item['link'].'" title="'.$item['title'].'"><strong>'.$item['title'].'</strong></a><br />
'.$item['description'].'<br />
Submitted by: <a href="'.$item['digg:submitter']['digg:userimage'].'">'.$item['digg:submitter']['digg:username'].'</a> :: '.$item['pubDate'].'<br />
Diggs: '.$item['digg:diggCount'].' :: <em>Category: '.$item['digg:category'].'</em>
</p>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Digg Front Page</title>
</head>
<body>
<?php
echo $html;
?>
</body>
</html>
I just want to include the corrected and working code in case anyone need it thanks to the amazing people who share and help on this awesome website
<?php
require_once 'rss_php.php';
header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML
$xml = new SimpleXMLElement('<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"></rss>');
$xml->addAttribute('version', '2.0');
$rss = new rss_php;
$rss->load('http://rss.tvguide.com/breakingnews');
$items = $rss->getItems();
$channel = $xml->addChild('channel'); //add channel node
$title = $channel->addChild('title','Channel Title Here'); //title of the feed
$link = $channel->addChild('link','http://www.zstreambox.com'); //feed site
$description = $channel->addChild('description','Channel description here'); //feed description
$language = $channel->addChild('language','en-us'); //language
$ttl = $channel->addChild('ttl','120'); //time
$image = $channel->addChild('image'); //add atom node
$url = $image->addChild('url','http://static.tvgcdn.net/www/img/tvguide-feed-logo.png'); //add channel image
$title = $image->addChild('title','ZSB: Breaking News'); //add channel title
$link = $image->addChild('link','http://www.zstreambox.com'); //add channel link
foreach($items as $index => $item) {
$xml_item = $channel->addChild('item'); //add item node
$title = $xml_item->addChild('title', ''.$item['title'].'');
$description = $xml_item->addChild('description', ''. $item['description'] . '');
}
echo $xml->asXML();
?>
Upvotes: 0
Views: 560
Reputation: 54831
Do you understand what's happening with $item
variable in your foreach
?
foreach($items as $index => $item) {
// here $item in a item of RSS feed
$item = $xml->addChild('item'); //add item node
// and here $item is an XML-node
// and as it is an XML-node now -
// it doesn't have `title` or `description` property
$title = $item->addChild('title', ''.['title'].''); //add title node
$description = $item->addChild('description', ''. ['description'] . ''); //add description
}
Proper code:
foreach($items as $index => $item) {
$xml_item = $xml->addChild('item'); //add item node
$title = $xml_item->addChild('title', ''.$item['title'].'');
$description = $xml_item->addChild('description', ''. $item['description'] . '');
}
Upvotes: 1
Reputation: 1539
Based on the example from the RSS parser, each $item
is an associative array with keys such as title
and description
. ['title']
is an array literal containing one string, 'title'
, if you want to access the title for an item, you would use $item['title']
giving you something like:
$title = $item->addChild('title', $item['title']); //add title node
Upvotes: 0