php 제작 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 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Laravel - 변환된 유효성 검사 규칙으로 API 요청 제공동적 콘텐츠를 위해 API를 통해 Laravel CMS에 연결하는 모바일 앱(또는 웹사이트) 구축을 고려하십시오. 이제 앱은 CMS에서 번역된 콘텐츠를 받을 것으로 예상되는 다국어 앱이 될 수 있습니다. 일반적으로 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.