Hi guys,

I'm looking to migrate a site that has a property listing. The data for the property listing is done via an FTP import of one or more XML files that updates once every hour or so.

I'm quite taken with Drupal and would like to use it to manage the site, however I don't know if there's any module or such that is capable of doing hourly grabs of property data over FTP.

Is this possible in Drupal or would I have to code something outside of drupal and use a cron job to import the data?

My thanks for any and all assistance given,
~Matt

Comments

paulhudson’s picture

Check out the Feeds module, it's excellent for this sort of thing: https://drupal.org/project/feeds

You can schedule imports, map XML to Drupal node fields. The. Just expose all the juicy data using views. :-)

MattBridger777’s picture

Found Feeds and it's brilliant and all.
There appears to be one rather significant problem however and that's that its FTP plugin doesn't appear to work. At the very least it appears to have a problem when the directory in the FTP settings is "/".

When the file its importing from is local, then it all works fine. But I need to be able to FTP the file from its origin to a local folder in this case.

I managed to use PHP's ftp_get() function successfully to copy the required file from source to the local destination. However this needs to be run every hour.

So my next question, if the FTP plugin for Feeds doesn't work, is there a way for me to run ftp_get() using cron, and if so can someone detail to me how I'd accomplish this?

My thanks and appreciation of any and all help given,
~Matt

paulhudson’s picture

I'm not sure about any Feeds FTP issue, you'd have to look at the issue queue on the project page for that :-)

You can use hook_cron() for your scheduled tasks:

/**
 * Implements hook_cron()
 */
function MYMODULE_cron() {
  // Your code here
}
MattBridger777’s picture

You'll have to forgive me if I ask you guys to spell this out for me in big, bold letters, but I am quite new at this.

I'm assuming that I put this cron function in template.php (which I have done). A copy of this you can see below, where my theme is called "adorecardiff" :-

function adorecardiff_cron ()
{
    $local_file = 'sites/default/files/feeds/properties.xml';
    $server_file = 'properties.xml';
    $ftp_server = "<ftp_server>";
    $ftp_user_name = "<username>";
    $ftp_user_pass = "<password>";

    // set up basic connection
    $conn_id = ftp_connect($ftp_server);

    // login with username and password
    if ( $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) )
    {
        // try to download $server_file and save to $local_file
        if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
            watchdog('ftp_cron_debug', "Successfully written to $local_file");
        } else {
            watchdog('ftp_cron_debug', "FTP GET failed");
        }
    }//end if
    else
    {
        watchdog('ftp_cron_debug', "FTP Login failed");
    }//end else
    // close the connection
    ftp_close($conn_id);
}//end function

After clearing the cache and running cron, nothing happened. I even tried removing the entire contents of the function and just replacing it with a watchdog statement saying "in here?" which also did not appear in the logs.

Have I missed a step or done something wrong?

My thanks for your patience and assistance,
~Matt

paulhudson’s picture

No worries,

You actually want to use hook_cron in a module file. :-)

Create a little custom module with your hook_cron implementation and you should be good to go.

Creating a module Docs: https://drupal.org/node/361112

Enjoy! :-)

MattBridger777’s picture

*revelatory* Ah ha. Now I see.

Thanks for that.
Since I'm so new to this I haven't had cause to get involved with module building and so it never occurred to me that was what you meant in the earlier comments.

Cheers muchly, I shall try it out.

~Matt

paulhudson’s picture

No worries, I should have been more specific. :-)