我是一个不熟练的PHP用户,试图生成XML RSS文档,
使用PHP类RSS_PHP RSS Parser
foreach
返回30 <items>
,这是好的,因为这是xml源上的项目数,但是当尝试获取<title>
的值时,结果是Array
1}}
我不知道我缺少什么或需要更改以从source而不是数组中获取<title>
的值。
非常感谢任何帮助,谢谢。
<?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>
PHP Class网站提供的示例是:
<?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>
我只想包含更正和正常工作的代码,以防有人需要它 感谢在这个很棒的网站上分享和帮助的了不起的人
<?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();
?>
答案 0 :(得分:1)
您是否了解$item
中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
}
正确的代码:
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'] . '');
}
答案 1 :(得分:0)
根据RSS parser中的示例,每个$item
都是一个关联数组,其中包含title
和description
等关键字。 ['title']
是一个包含一个字符串'title'
的数组文字,如果您想要访问某个项目的标题,您可以使用$item['title']
为您提供以下内容:
$title = $item->addChild('title', $item['title']); //add title node