【问题标题】:JSON Feed to RSS (Problems)JSON Feed 到 RSS(问题)
【发布时间】:2020-02-20 15:01:04
【问题描述】:

我在将 JSON 提要转换为 RSS 提要时遇到问题。我正在尝试在单个函数中执行此操作,如下所示。

我在 GitHub 上找到了代码——但它不能正常工作——我不知道如何修复它。

在PHP代码底部:echo convert_jsonfeed_to_rss($content)."\n"; 我希望这会打印出 RSS XML 提要——它不会。

<?php

//Convert JSONfeed to RSS in a single function as a drop in to make adding JSONfeed
//support to an aggregator easier
function convert_jsonfeed_to_rss($content = NULL, $max = NULL)
{
    //Test if the content is actual JSON
    json_decode($content);
    if( json_last_error() !== JSON_ERROR_NONE) return FALSE;

    //Now, is it valid JSONFeed?
    $jsonFeed = json_decode($content, TRUE);
    if (!isset($jsonFeed['version'])) return FALSE;
    if (!isset($jsonFeed['title'])) return FALSE;
    if (!isset($jsonFeed['items'])) return FALSE;

    //Decode the feed to a PHP array
    $jf = json_decode($content, TRUE);

    //Get the latest item publish date to use as the channel pubDate
    $latestDate = 0;
    foreach ($jf['items'] as $item) {
        if (strtotime($item['date_published']) > $latestDate) $latestDate = strtotime($item['date_published']);
    }
    $lastBuildDate = date(DATE_RSS, $latestDate);

    //Create the RSS feed
    $xmlFeed = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"></rss>');
    $xmlFeed->addChild("channel");

    //Required elements
    $xmlFeed->channel->addChild("title", $jf['title']);
    $xmlFeed->channel->addChild("pubDate", $lastBuildDate);
    $xmlFeed->channel->addChild("lastBuildDate", $lastBuildDate);

    //Optional elements
    if (isset($jf['description'])) $xmlFeed->channel->description = $jf['description'];
    if (isset($jf['home_page_url'])) $xmlFeed->channel->link = $jf['home_page_url'];

    //Items
    foreach ($jf['items'] as $item) {
        $newItem = $xmlFeed->channel->addChild('item');

        //Standard stuff
        if (isset($item['id'])) $newItem->addChild('guid', $item['id']);
        if (isset($item['title'])) $newItem->addChild('title', $item['title']);
        if (isset($item['content_text'])) $newItem->addChild('description', $item['content_text']);
        if (isset($item['content_html'])) $newItem->addChild('description', $item['content_html']);
        if (isset($item['date_published'])) $newItem->addChild('pubDate', $item['date_published']);
        if (isset($item['url'])) $newItem->addChild('link', $item['url']);

        //Enclosures?
        if(isset($item['attachments'])) {
            foreach($item['attachments'] as $attachment) {
                $enclosure = $newItem->addChild('enclosure');
                $enclosure['url'] = $attachment['url'];
                $enclosure['type'] = $attachment['mime_type'];
                $enclosure['length'] = $attachment['size_in_bytes'];
            }
        }
    }

    //Make the output pretty
    $dom = new DOMDocument("1.0");
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($xmlFeed->asXML());
    return $dom->saveXML();
}


$content = @file_get_contents("http://timetable.manton.org/feed.json");
echo convert_jsonfeed_to_rss($content)."\n";

我试图将$content 变量放在convert_jsonfeed_to_rss 函数之前——同样的问题。

$content = @file_get_contents("http://timetable.manton.org/feed.json");

【问题讨论】:

    标签: php json rss


    【解决方案1】:

    最后转换为 DOMDocument 似乎是个问题,它反对文档的某些部分。

    一种更快的方法(在这种情况下似乎更可靠)是只使用dom_import_simplexml()

    $dom = dom_import_simplexml($xmlFeed)->ownerDocument;
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    return $dom->saveXML();
    

    (来自https://stackoverflow.com/a/799792/1213708)。

    编辑

    从您更新的代码来看,您没有正确使用层次结构(并且可能没有启用错误来显示什么不工作)。

    你需要...

    foreach ($jf['data']['feed'] as $item1) {
    //      foreach ($item1['feed'] as $item) {
    
            $item = $item1['article'];
    

    【讨论】:

    • 嗨,谢谢您的帮助,现在可以了,但是我在将我的 JSON 脚本实现到该程序时遇到问题,请参阅我的代码:pastebin.com/70yNggze 我添加了 2 个 foreach 函数,第一个工作,但第二个不工作这里是我的 JSON 提要:pastebin.com/fhWt1ery@Nigel Ren
    • 我已经更新了答案,您需要检查,因为还有其他错误,但循环应该会生成一些数据。
    • 现在可以工作,但我需要在项目 rss 部分添加缩略图 url。问题是浏览器不添加媒体:在缩略图之前只添加 example.com/images.jpg"> 我需要强制浏览器在缩略图之前添加媒体,如下所示:example.com/image.jpg"> 我尝试:如果(isset($item['image_url'])) $newItem->addChild('media:thumbnail url="'.$item['image_url'].'"');但不在缩略图之前添加媒体? @Nigel Ren
    • 编辑:我在描述部分使用 CDATA 并修复了图像问题,在之前的 foreach 函数中我看到没有从 JSON 打印标签和类别:pastebin.com/fhWt1ery 我看到我需要添加标签和类别的新 foreach 对吗? @Nigel Ren
    • 如果你有多个数据,比如标签和类别,使用另一个级别的foreach()会更容易。
    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多