2011-03-03 5 views
0

PHP에서 RSS를 생성하지 않고도 PHP를 통해 RSS 파일에 새로운 item 요소를 추가해야합니다. 이전 항목을 제거해야하고 표시 할 번호와 일치해야한다는 것을 알고 있지만 먼저 파일에 추가하는 방법을 모르겠습니다.PHP를 통해 RSS를 생성하지 않고 RSS 파일에 추가 할 PHP

내 코드는 다음과 다소 같습니다

<rss version="2.0"> 
    <channel> 
    <title>My Site Feed</title> 
    <link>http://www.mysitethathasfeed.com/feed/</link> 
    <description> 
     A nice site that features a feed. 
    </description> 
    <item> 
     <title>Launched!</title> 
     <link>http://www.mysitethathasfeed.com/feed/view.php?ID=launched</link> 
     <description> 
      We just launched the site! Come join the celebration! 
     </description> 
    </item> 
    </channel> 
</rss> 
+0

에서 카일 (오 OOP의 상속)에서 답을 확장하고 참조를 의미합니까? 기존 문서를 구문 분석하고 조작 한 다음 RSS를 다시 생성하여 출력해야하는 것처럼 들립니다. – deceze

+0

내 뜻은 액세스 할 때 콘텐츠가 생성되지 않는다는 것입니다. RSS가 필요할 때 추가됩니다. RSS가 액세스 될 때 PHP 처리가 필요하지 않습니다. –

+0

@Tanner : 나는 당신이 여기서하려고하는 것을 이해합니다. PHP에 의해 조작 된 정적 RSS 파일을 PHP 스크립트의 실행에 의해 다이내믹하게 만들어지는 파일이 아닌 정적 파일로 다시 저장합니다 . 이 접근법을 취하는 이유가 있습니까? PHP 스크립트의 실행을 통해 RSS 출력을 즉각적으로 생성하는 것이 동적 컨텐츠를 관리하는 훨씬 더 좋은 방법입니다. –

답변

0

PHP Manual

<?php 

$rss = file_get_contents('feed.rss'); 
$dom = new DOMDocument(); 
$dom->loadXML($rss); 

// should have only 1 node in the list 
$nodeList = $dom->getElementsByTagName('channel'); 

// assuming there's only 1 channel tag in the RSS file: 
$nChannel = $nodeList->item(0); 

// now create the new item 
$newNode = $dom->createElement('item'); 
$newNode->appendChild($dom->createElement('title', 'a new title post')); 
$newNode->appendChild($dom->createElement('link', 'http://www.mysitethathasfeed.com/feed/view.php?ID=launched')); 
$newNode->appendChild($dom->createElement('description', 'This is the 2nd post of our feed.')); 

// add item to channel 
$nChannel->appendChild($newNode); 
$rss = $dom->saveXML(); 
file_put_contents('feed.rss', $rss); 
1
// Load the XML/RSS from a file. 
$rss = file_get_cotents('path_to_file'); 
$dom = new DOMDocument(); 
$dom->loadXML($rss); 

사용 http://php.net/manual/en/book.dom.php 당신이로드 DOM을 수정하는 방법을 배울 수 있습니다. 당신은 "RSS를 생성하지 않고"무엇을 ​​

관련 문제