用PHP将博客的RSS数据转换为JSON格式

wordpress虽然已经提供了Restful API,但对于某些自定义的文章类型,要想获取它们的Json数据的话,我们还需要用到PHP来写一段程序。

本文提供的代码可以获取在wordpress或者blogspot的文章数据,生成的json文件可以用于制作APP。我的一个项目用到了wp-job-manager,我希望能够针对每一个店铺的搜索结果,获取这个店铺独立的RSS feed,然后用PHP将其生成json文件,然后再利用AnglarJS做前端就好办了。

首先我们新建一个文件 feed.php 代码如下:

header('Content-Type: application/json; Charset=UTF-8');
$feed = new DOMDocument();
$feed->load('http://someblogsite/feed/'); // put your feed url here
$json = array();

$json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;


$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');

$json['item'] = array();
$i = 0;


foreach($items as $item) {
   $title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
   $description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
   $pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
   $guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;
   
   $json['item'][$i++]['title'] = $title; 
   $json['item'][$i++]['description'] = $description;
   $json['item'][$i++]['pubdate'] = $pubDate;
   $json['item'][$i++]['guid'] = $guid;    

}

echo json_encode($json);

将代码中的网址更改为你自己的feed地址即可(wp-job-manager的RSS feed可以通过job board的搜索功能获取),然后将该文件上传到服务器,在浏览器中输入http://www.youdomain.com/feed.php就可以看到效果了。

下面,我们需要更改URL的结构,将 http://www.youdomain.com/feed.php 改为 http://www.youdomain.com/feed.json 因为 JSON API 需要 .json  的文件,在上述代码的 echo json_encode($json); 之前加上:

//create a new array out of it
file_put_contents('feed.json', json_encode($json));
$json_string = json_encode($json);
$file = 'feed.json';
file_put_contents($file, $json_string); 

现在,在浏览器地址栏输入 http://yourdomain.com/feed.json 查看效果,.json 文件就生成好了。完整代码如下:

<?php
header('Content-Type: application/json; Charset=UTF-8');
$feed = new DOMDocument();
$feed->load('http://someblogsite/feed/');// put your feed url here
$json = array();
$json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
$json['item'] = array();
$i = 0;
foreach($items as $item) {
   $title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
   $description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
   $pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
   $guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;
   
   $json['item'][$i++]['title'] = $title; 
   $json['item'][$i++]['description'] = $description;
   $json['item'][$i++]['pubdate'] = $pubDate;
   $json['item'][$i++]['guid'] = $guid;    
//create a new array out of it
file_put_contents('feed.json', json_encode($json));
$json_string = json_encode($json);
$file = 'feed.json';
file_put_contents($file, $json_string);
}
echo json_encode($json);
?>

生成.json文件的时候,需要注意文件和文件夹的权限,你需要将包含feed.php的各级目录权限更改为可写,并将feed.php文件本身的权限改为777。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注