Duke Yin's Technology database

PHP解析WordPress站点RSS

WordPress默认会在 “网址/feed”上输出RSS ,是一种xml格式。

使用PHP可以进行读取、解析、获得某个部分的内容。如下:

$url = "https://www.wpsitedomain.com/feed"; //乱编的rss url,仅供举例
$rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
$namespaces = $rss->getNamespaces(true);
$posts = $rss->channel->item;
foreach($posts as $post){
$title = $post->title;
$content =  $post->children($namespaces['content'])->encoded;
$link = $post->link;
$category = $post->category; //array
$description = $post->description; 
}

变量posts就是所有获取到的文章对象,数量取决于站点设置,把posts进行遍历以后,就可以单独取各个文章的某个部分。

# #

发布评论

评论

标注 * 的为必填项。