1

I currently use Feed-Me to pull data from an API-Endpoint. This API-Endpoint does change from time to time, providing new elements with each change.

I don't want to manually trigger the Feed-Me-Reimport each time I notice a change. I do know that Feed-Me provides actions URLs for each feed-entry but as the amount of different import-feeds will grow rapidly soon, I don't think it's feasable to manually create those feeds, and after creation also manually put the URL into a custom cronjob.

So in the end what I'd like to have is simply a script that is triggered by a single cronjob, then gets all Feed-Me feeds and for each feed run the reimport.

Via PluginFactory I already generated a plugin that provides me an endpoint(/controller) to be called by cron

public function actionAutoImport()
    {
        $result = 'Autoimport engaged';

        return $result;
    }

But now I am wondering: How do I get information from the Feed-Me plugin i.e. get all existing feeds and trigger a run on each?

2 Answers 2

4

You should be able to use something similar to the following:

use Craft;
use verbb\feedme\FeedMe;
use verbb\feedme\queue\jobs\FeedImport;

$feeds = FeedMe::$plugin->feeds->getFeeds();

foreach ($feeds as $feed) {
    $limit = null;
    $offset = null;
    $processedElementIds = [];

    Craft::$app->getQueue()->delay(0)->push(new FeedImport([
        'feed' => $feed,
        'limit' => $limit,
        'offset' => $offset,
        'processedElementIds' => $processedElementIds,
    ]));
}

The above will add all your feeds to Craft's queue system and start them.

2
  • can you pass a url to the feed using this method? I'm currently using a pretty hacky way to trigger a feed programatically -> craftcms.stackexchange.com/questions/32933/…
    – dmatthams
    Commented Nov 21, 2019 at 11:33
  • Small update for newer versions of the Feedme plugin: $feeds = \craft\feedme\Plugin::$plugin->getFeeds()->getFeeds();
    – Mato
    Commented Sep 28, 2021 at 10:03
1

It's worth mentioning now that for more recent versions of FeedMe, the console command natively supports an all flag.

https://docs.craftcms.com/feed-me/v4/feature-tour/trigger-import-via-cron.html#console-command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.