6

I can successfully import remote content using the Feeds module by manually clicking on the Import button. I need to execute the import process from code, either from a module or executing PHP code from the Rules module.

I have tried the following code in a custom module.

function imports_cron_cron(){
  drupal_set_message('Import-CRON responding to cron execution! ...');
  $importer_id = 'publications_import';
  $url = "http://xml/url";
  $feeds_source = feeds_source($importer_id);
  $feeds_config = $feeds_source->getConfigFor($feeds_source->importer->fetcher);
  $feeds_config['source'] = $url;
  $feeds_source->setConfigFor($feeds_source->importer->fetcher, $feeds_config);
  $feeds_source->save();
  $feeds_source->startImport();

  drupal_set_message(' .... ENDING IMPORTS-CRON CALL!');
}

Both the drupal_set_message() calls are successfully executed, but no node is imported.

Can anyone help, please?

3 Answers 3

13

Your code seems to be OK. I use some similar code in one of my projects (this a csv importer, similar concept):

function custom_function($importer_id, $csvfile_path) {
  // Load the Feeds Source object.
  $source = feeds_source($importer_id);
  // Load the source fetcher config.
  $fetcher_config = $source->getConfigFor($source->importer->fetcher);
  $fetcher_config['source'] = $csvfile_path;
  $source->setConfigFor($source->importer->fetcher, $fetcher_config);
  $source->save();
  $source->startImport();
}

Make sure that you are using the foreground configuration, this way the batch process could be be executed "inside" drupal with showing the progress bar. Or you can force it:

// Tweak the importer configuration, to enable "Process in the background".
$config = array(
  'process_in_background' => TRUE,
);
$source->importer->addConfig($config);

// Execute the import.

Another approach could be to schedule it

// Schedule the import, and make sure importer is scheduled, too.
$source->schedule();
$source->importer->schedule();

This is a good article about feeds importer

1

Ive seen lots about this. Heres my take , I want to be able to do this from the command line, and be able to do it from an install profile.

so taking hints from previous answers, and from the documentation Ive seen / found, Ive come to the realisation that

dont use the batch api for this unless

a) you're doing this from the form api (a user clicks)

or

b) you're writing a drush command.

so this is what Ive got working in an module install

function load_data(){

// Files to import in specific order.
  $files = array(
    'challenge_importer' => 'data/challenges.csv',
);

$file_location_base = drupal_get_path('module', 'challenge');
foreach ($files as $feed => $file) {

$feedSource = feeds_source($feed);
// Set the file name to import
$filename = $file_location_base.'/' . $file;
if (!file_destination($filename, FILE_EXISTS_ERROR)) {
  $config = $feedSource->getConfig();
  $config['FeedsFileFetcher']['source'] = $filename;
  $feedSource->setConfig($config);
  $feedSource->save();
    while (FEEDS_BATCH_COMPLETE != $feedSource->import());
    }
  }
}

where

  • challenge_importer is a feeds importer,
  • challenge is the module.
  • data is stored in module/data

    and with this structure you can have multiple files imported in a defined order.

0

I believe you need to do something more like

$node = new stdClass();
$node->type = 'feed';
$node->title = 'My feed';
$node->feeds['FeedsHTTPFetcher']['source'] = 'http://example.com/rss.xml';
node_save($node);

You should take a look at https://drupal.org/node/622700 (Programmatic operations).

2
  • Thank you Steven but I am looking for a stand-alone form solution rather than a node based solution.
    – sisko
    Oct 24, 2013 at 19:40
  • If that's what you are looking for I believe what you are trying to do should work. Try adding some watchdog functions on variables to see what the variables contain.
    – Steven
    Oct 25, 2013 at 13:01

Your Answer

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

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