php 제작 xml 기반 RSS 구독 원 기능 예시

3785 단어 phpxmlRSS
이 사례 는 phop 제작 xml 기반 RSS 구독 원 기능 을 다 루 고 있다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
먼저 RSS 템 플 릿 을 만 듭 니 다.템 플 릿 의 파일 이름 은 feed.xml 이 고 코드 는 다음 과 같 습 니 다.

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>

또한 php 파일 로 데이터베이스 에서 데 이 터 를 읽 고 RSS 파일 을 생 성 합 니 다.데이터베이스 에서 읽 은 데 이 터 를 배열 로 모 의 합 니 다.

<?php 
class Rss{
  protected $dom = null;
  protected $temp = './feed.xml';
  protected $rss = null;
  protected $title = '';
  protected $desc = '';
  protected $link = '';
  public function __construct(){
    $this->title = '   ';
    $this->desc = '     ';
    $this->link = 'http://mysql/rss.php';
    $this->dom = new DOMDocument('1.0','utf-8');
    $this->dom->load($this->temp);
    $this->rss = $this->dom->getElementsByTagName('rss')->item(0);
  }
  public function feed($arr){
    $this->createChannel();
    $channel = $this->dom->getElementsByTagName('channel')->item(0);
    foreach ($arr as $v){
      $channel->appendChild($this->createItem($v));
    }
    header('content-type:text/xml');
    echo $this->dom->savexml();
  }
  protected function createChannel(){
    $channel = $this->dom->createElement('channel');
    $channel->appendChild($this->createEle('title',$this->title));
    $channel->appendChild($this->createEle('link',$this->link));
    $channel->appendChild($this->createEle('description',$this->desc));
    $this->rss->appendChild($channel);
  }
  protected function createItem($arr){
    $item = $this->dom->createElement('item');
    foreach($arr as $k => $v){
      $item->appendChild($this->createEle($k,$v));
    }
    return $item;
  }
  protected function createEle($name,$value){
    $e=$this->dom->createElement($name);
    $t=$this->dom->createTextNode($value);
    $e->appendChild($t);
    return $e;
  }
}
$arr = array(
  array(
    'title'=>'    ',
    'link'=>'1',
    'description'=>'    '
  ),
  array(
    'title'=>'   ',
    'link'=>'1',
    'description'=>'        '
  )
);
$rss = new Rss;
$rss->feed($arr);
?>

마지막 으로 불 여우 아래 효과:

PS:여기 서 xml 작업 에 관 한 온라인 도 구 를 몇 가지 더 제공 하여 참고 하 시기 바 랍 니 다.
온라인 XML/JSON 상호 변환 도구:
http://tools.jb51.net/code/xmljson
온라인 포맷 XML/온라인 압축 XML:
http://tools.jb51.net/code/xmlformat
XML 온라인 압축/포맷 도구:
http://tools.jb51.net/code/xml_format_compress
XML 코드 온라인 포맷 미화 도구:
http://tools.jb51.net/code/xmlcodeformat
더 많은 PHP 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 논문 에서 말 한 것 이 여러분 의 PHP 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기