How to schedule a task in PHP once a day

Hi there,

I am working on a php project on my WAMP server.

I would like to schedule a task for accessing a fresh xml file once in a day from a website and replace (or update the old xml file) residing on my computer. The old xml file is saved on my localhost as an archived copy.

How can I schedule this task automatically and make php automatically access the new file from a website and update my xml file once a day.

All comments and feedbacks are always welcomed :slight_smile:

Thank you!

set a cron job.

Thanks for the reply shaydez. I am newbie, how do I set a cron job on my wamp server.

kindly reply.

Thank you :slight_smile:

Wamp is on windows right, so I think you might have to use windows scheduling or some other software like that to have it call your script in certain time intervals.

I don’t know what windows version you have but here is microsoft tutorial on scheduler for XP

thanks for the reply. I can create a task on my windows 7 but how would I configure the php script that will read the fresh xml file and (update or replace) the one on my computer.

Kindly reply.

Thank you :slight_smile:

Well that is a little bit more complex task. But basically I would look into some tutorials on reading/writing files in php , also check out file_get_contents() , that will let your read a file into variable in php.

And for parsing XML in php look into http://www.php.net/manual/en/book.simplexml.php

I’ve used a program called nn_cron on windows that let’s you schedule PHP script execution. More flexible than Windows task scheduler.

Thanks for the reply.

I can now read the xml file from a web link using file_get_contents() from my php code.

And it displays exactly what I need. No problems.

Now I can use this weblink to get the updated xml data only once in a day. I need to put this php script in a windows scheduler.

Please reply. How i can do it.

The code that I use to access the xml from a website is:

 
//this is the link I access once in a day
$xml_file = "http://mydomain.com/TheUpdatedxmlFile.xml";

    $xmlstr = file_get_contents($xml_file);

    if (!empty($xmlstr) && !empty($SetCurrency))
    {
        $xml = new SimpleXMLElement($xmlstr);
        $currencies = array();
        foreach ($xml->currency AS $a_currency) {
            $csymbol = $a_currency->csymbol;
            $crate = $a_currency->crate;
            $currencies["$csymbol"] = $crate;
        }

        $c_rate = $currencies[AUD];
    }

Please reply I need to convert this into some script and put this in the windows scheduler.

Thank you :slight_smile:

Thank you.

Thanks cranial-bore, could you please share some sample code on how to run this program nn_cron.

I am accessing the updated xml file via the link as:

//this is the link I access once in a day
$xml_file = "http://mydomain.com/TheUpdatedxmlFile.xml";

    $xmlstr = file_get_contents($xml_file);

    if (!empty($xmlstr) && !empty($SetCurrency))
    {
        $xml = new SimpleXMLElement($xmlstr);
        $currencies = array();
        foreach ($xml->currency AS $a_currency) {
            $csymbol = $a_currency->csymbol;
            $crate = $a_currency->crate;
            $currencies["$csymbol"] = $crate;
        }

        $c_rate = $currencies[EUR];
    }

Now this code will run everytime the page is loaded. I need to convert this into some script and perhaps use it through the program u suggested ‘nn_cron’ once in a day and update the one on my local machine.

So basically I just need a code that will replace (or update) the old xml on my localhost with the fresh xml file through a link i.e.

http://mydomain.com/TheUpdatedxmlFile.xml

And then once I have the xml file on my localhost updated, I will simply read from the localhost using the code I pasted above.

So I just need a script that will update the old xml file with the new xml file through some scheduler. You suggested nn_cron, so whats the best way to do we do update files thru nn_cron.

Please reply. Thank you :slight_smile:

NN cron just executes a PHP script on your machine, so the only “NN Cron Code” is that which schedules the execution.

Intall NN Cron Lite and then edit the cron.tab file (in the installation directory)
Here’s a guide to the syntax of a cron.tab file

So the line in the cron.tab to execute a PHP script on your computer at 2:15pm, 3:15pm and 4:15pm every Tuesday would look like this:


15 14-16 * * 2 php -q c:\\path\	o\\myphpfile.php

Your PHP file may need to use absolute paths, as it will be executing relative to where PHP is installed.
It’s like executing PHP via the command line on a schedule.

Thanks for your reply cranial-bore,

could you please guide me about writing a code that will replace the old file with the new one.

So the code has to do 2 steps:

1- Access a new file thru a link.
2- Copy or save the new file to the localhost (and replace (or update) the old file with the new one)

Please reply.

Thank you very much :slight_smile:

To get the file from a remote server, I would use cURL and then fopen, fwrite, fput (or file_put_contents) to write to a local file.

its always better to put full-path for the PHP, when it runs as a cron job, cron demon may not able to find the correct PHP path to execute


15 14-16 * * 2 /usr/bin/php -q c:\\path\	o\\myphpfile.php