Export RSS feeds to remote ftp sites from your Drupal website using Views

| | 2 min read

Feed API provides a powerful means of importing feeds from other sites directly into your Drupal site. What if you want to export your feed as an XML file to a content distributor who wants it on his FTP site? There is of course the traditional approach of using curl to read your feed URL and dump the text as files on to the ftp site. There is not much control that you have over this process. However doing this via Drupal Views will give you fine grained control over the process

First create a view and then add a feed display. Set all the basic parameters for the feed display to suit your choice. Make sure that you set some sort order that will ensure that the records will be selected from a predictable queue. Now add one parameter that will uniquely identify each of the items in the feed and also allows you to move along the queue given the sort order.

If the feed is a feed of node teasers use the Node Id (nid) as a filter parameter. Set appropriate criteria for the filter value. For example if you have a sort order as nid ascending then use a filter criteria as 'nid > value'. When you pass nid=some-value, your feed will display items_per_page nodes with nid greater than some-value. Now you are all set to use the view in some code that can publish this feed to the ftp site.

The next task is to use this view in some piece of code that will be called on cron run which will programmatically create the view, get the content, and write the content as xml files to the ftp site. The following piece of code demonstrates the views part of the job.

$view = views_get_view('name_of_view');
$view->set_display($display_id);
$filter = $view->get_item($display_id, 'filter', 'name_of_filter');
// Start from where we left off last time.
$filter['value']['value'] = $last_exported_nid + 1;
$view->set_item($display_id, 'filter', 'name_of_filter', $filter);
// Set the number of items you wish to export.
$view->display_handler->set_option('items_per_page', 100);
// Call to preview to ensure that the items per page is respected.
$view->preview();
$view->execute();
// Get the RSS content from the view.
$data = $view->render();

Handing the ftp part of the job should be child's play given that you have the views part figured out now :-). If you need help get in touch with us.