JSON Feed Plugin

Yeap, they support JSON since a few days. @texnixe posted the blog post where they announced the support.

Cool. Currently on a boat. Will look into that when I’m home

FWIW, its also supported by NewsBlur

One difference I noticed to your implementation: My “feed_url” is empty. Could that be the cause? And if so, how to fix that?

Update: I just added the feed url directly into the json_feed.php. I know thats probably not correct, but at least “feed_url” is now filled correctly. My JSON feed seems to work in Newsblur right now. But its still not working in Feedbin. So now its more likely an error on Feedbin site (IMHO its a caching issue - they still deliver the corrupt feed, even when I resubscribe to the feed). I currently try to contact them.

@rrrrfischer Thanks for the update. Will update the source on Github so there’s a clear description how the feed_url should be set.

Edit: Updated the README with some documentation about the options you can pass to the method: https://github.com/stefanzweifel/kirby-json-feed#usage

1 Like

@stefanzweifel Yeap, it was a bug within Feedbin - which is now fixed.

During the test, I’ve found another glitch: Feedbin requires the “date_published” entry. Otherwise all entries are displayed with the time Feedbin was catching the feed/article. Currently there is only the “date_modified” key inside the feed. It would be great if you would add the “date_published” key to the feed :slight_smile:

Update: As I recognized, that “date_modified” already contained the publish date (instead of the the last update date), I just changed “date_modified” to “date_published” inside the “json-feed.php”.

So, IMHO there is just a solution needed, for the “date_modified” key (as it currently contains the publish date).

Thanks for the info.
Created an issue and published a new release yesterday.

Now both keys (date_modified and date_published) are in the item list. (But I know that their not the same and should probably be a separate field)

1 Like

Thanks! Is there any way to write the real (last) modified date into the “date_modified” key? Currently it seems that both keys contain the publish date?!

Well, not by default.

The Plugin currently just uses the “date” field (Link
https://github.com/stefanzweifel/kirby-json-feed/blob/master/json-feed.php#L30-L31).
Kirby doesn’t have the concept of “published date” and “modified date” by
default. You could however add a new field called “date_modified” in your
blueprints, set it in your pages and adjust the json-feed plugin.

The plugin code should be simple enough so everyone can add or remove the
fields he or she wants. If the JSON feed would get a lot of traction I
would add all remaining fields. But currently I think thats an overkill.

René Fischer getkirby@discoursemail.com schrieb am Di., 30. Mai 2017 um
14:05 Uhr:

Okay, accepted! IHMO that key is not that necessary (at least for me), as most feed readers track updated feeds / articles themselves. So I live without the “date_modified” key for now :slight_smile:

You could also use $page->modified(). This timestamp is updated when a page is modified via the Panel.

Nice. Didn’t know about that.

Hey @stefanzweifel, kudos for the json plugin! Looks great!

I was just thinking if the counterpart is doable. Would it be possible to READ/PARSE a JSON Feed on Kirby? Do anyone know if a plugin for that exists?

I’ve quickly scrambled the following code together. I reads a Feed, parses the JSON and loops over all items.

    $feed = json_decode(file_get_contents('https://stefanzweifel.io/feed.json'));
    $items = $feed->items;

    foreach ($items as $item) {

        echo $item->title;
        echo $item->content_html;
        echo $item->url;
        echo $item->date_published;

    }

(You could please this in Kirby Controller)

But keep in mind that in a production ready application I would advise to cache the JSON Feed for x amount of time :wink:

Very cool. Thanks for sharing!

Hi @stefanzweifel, thank you very much for the plugin! It really is great to be at the forefront of something new…

Now, I encountered an issue similar to Bastian’s XML feed plugin: The respective date of the entry is correct, but the time is not, it’s always midnight. This is due to the fact that I have separate fields for each of my blog entries: “date” and “time.” Is there a way to combine them either in the template or the json-feed.php file?

Many thanks in advance!

See this post: ISO Dates with custom fields

@insintesi you could use the technique @texnixe mentioned and then update the date_published field in json-feed.php to use the combined date.

I’m currently on a client project and hope to release a better version of the json-feed plugin after that project.

I’m sorry to bother again, but I haven’t managed to implement the separate time field successfully. Could anyone please tell me how to modify the date_published exactly and how to insert the combined isoDate from @texnixe?

<?php

Pages::$methods['jsonfeed'] = function ($pages, $params = []) {

    // set all default values
    $defaults = [
        'title'       => 'Feed',
        'feed'        => url(),
        'datefield'   => 'date',
        'textfield'   => 'text',
        'timefield'   => 'time',
    ];

    // merge them with the user input
    $options = array_merge($defaults, $params);

    $items = [];

    foreach ($pages as $item) {
        $items[] = [
            'id'             => $item->url(),
            'url'            => $item->url(),
            'title'          => $item->title()->value(),
            'content_html'   => $item->{$options['textfield']}()->kirbytext()->value,
            'date_published' => $item->date('c', $options['datefield']),
        ];
    }

    // Set the Header
    header::type('application/json');

    // Build the response
    $feed = [
        'version'       => 'https://jsonfeed.org/version/1',
        'title'         => $options['title'],
        'home_page_url' => url(),
        'feed_url'      => $options['feed'],
        'items'         => $items,
    ];

    return json_encode($feed);
};

$options[‘datefield’] just gives you the name of the field.

<?php
$date = $item->date('Y-m-d', $options['datefield']);
$time = $item->{$options['timefield']};
$isoDate = date('c', strtotime("$date $time"));