2016-01-19 17 views
0

我是一個不熟練的PHP用戶試圖生成一個XML RSS文檔,
使用PHP類RSS_PHP RSS Parserforeach導致數組中,我怎麼能得到元素的值而不是數組?

foreach收益30 <items>這是很好的,因爲這是這是項目的XML源上的數字,但是當試圖讓<title>結果這是一個Array

我不知道我失蹤或者我需要改變爲從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 
 

 
\t $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>'); 
 
\t $xml->addAttribute('version', '2.0'); 
 

 
\t $rss = new rss_php; 
 
    $rss->load('http://rss.tvguide.com/breakingnews'); 
 
    $items = $rss->getItems(); 
 
\t 
 
\t $channel = $xml->addChild('channel'); //add channel node 
 
\t $title = $channel->addChild('title','Channel Title Here'); //title of the feed 
 
\t $link = $channel->addChild('link','http://www.zstreambox.com'); //feed site 
 
\t $description = $channel->addChild('description','Channel description here'); //feed description 
 
\t $language = $channel->addChild('language','en-us'); //language 
 
\t $ttl = $channel->addChild('ttl','120'); //time 
 
\t $image = $channel->addChild('image'); //add atom node 
 
\t $url = $image->addChild('url','http://static.tvgcdn.net/www/img/tvguide-feed-logo.png'); //add channel image 
 
\t $title = $image->addChild('title','ZSB: Breaking News'); //add channel title 
 
\t $link = $image->addChild('link','http://www.zstreambox.com'); //add channel link 
 

 

 
\t foreach($items as $index => $item) { \t \t 
 
\t \t $item = $channel->addChild('item'); //add item node 
 
\t \t $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類網站提供的是:

<?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 
 

 
\t $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>'); 
 
\t $xml->addAttribute('version', '2.0'); 
 

 
\t $rss = new rss_php; 
 
    $rss->load('http://rss.tvguide.com/breakingnews'); 
 
    $items = $rss->getItems(); 
 
\t 
 
\t $channel = $xml->addChild('channel'); //add channel node 
 
\t $title = $channel->addChild('title','Channel Title Here'); //title of the feed 
 
\t $link = $channel->addChild('link','http://www.zstreambox.com'); //feed site 
 
\t $description = $channel->addChild('description','Channel description here'); //feed description 
 
\t $language = $channel->addChild('language','en-us'); //language 
 
\t $ttl = $channel->addChild('ttl','120'); //time 
 
\t $image = $channel->addChild('image'); //add atom node 
 
\t $url = $image->addChild('url','http://static.tvgcdn.net/www/img/tvguide-feed-logo.png'); //add channel image 
 
\t $title = $image->addChild('title','ZSB: Breaking News'); //add channel title 
 
\t $link = $image->addChild('link','http://www.zstreambox.com'); //add channel link 
 

 

 
\t foreach($items as $index => $item) {   
 
    \t $xml_item = $channel->addChild('item'); //add item node 
 
    \t $title = $xml_item->addChild('title', ''.$item['title'].''); 
 
    \t $description = $xml_item->addChild('description', ''. $item['description'] . ''); 
 
\t } 
 

 
echo $xml->asXML(); 
 

 
?>

+0

那麼,哪裏'title'從何而來? –

+0

在創建它之後不會附加到根元素 – charlietfl

+0

@charlietfl謝謝..很抱歉,請問您,請給我更多關於如何附加到根文檔的信息?謝謝。 – Peter

回答

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'] . ''); 
} 
+0

非常感謝你的幫助,解釋和解決方案的代碼,它的工作原理,你不知道你有多少幫助我,謝謝! – Peter

0

例如來自RSS parser的示例,每個$item是具有諸如titledescription之類的關鍵字的關聯陣列。 ['title']是一個數組常量,其中包含一個字符串,'title',如果你要訪問的標題項,你可以使用$item['title']給你這樣的:

$title = $item->addChild('title', $item['title']); //add title node 
+0

非常感謝@mzulch我替換了 '$ title = $ item- > addChild('title',''。['title']。''); //添加標題節點' '$ title = $ item-> addChild('title',$ item ['title']); //添加標題節點' 該數組已刪除,但結果爲空'' 您可以在我的網站上看到它:[link] http://zstreambox.com/mkttools/rssphp/t1.php [link ] – Peter

相關問題