# Thread: How to schedule a task in PHP once a day.

1. ## 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

Thank you!

2. set a cron job.

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

Thank you

4. 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
http://support.microsoft.com/kb/308569

5. 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.

Thank you

6. 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

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

8. Originally Posted by hostentic
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
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:

PHP Code:
 //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 Thank you. 9. Originally Posted by cranial-bore 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 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: PHP Code:  //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.

PHP Code:
 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.

10. 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:
Code:
15 14-16 * * 2 php -q c:\path\to\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.

11. Originally Posted by cranial-bore
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:
Code:
15 14-16 * * 2 php -q c:\path\to\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.

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)

Thank you very much

12. 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.

13. Originally Posted by cranial-bore
Code:
15 14-16 * * 2 php -q c:\path\to\myphpfile.php
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

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•