SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Melbourne
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    set a cron job.
    Sr. Website Developer and Internet Marketing
    www.CarlosJa.com Note: If anyone
    needs to get ahold of me please feel free to email me through
    my site. Apparently i missed quite a few private messages.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Melbourne
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shaydez View Post
    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

  4. #4
    SitePoint Enthusiast hostentic's Avatar
    Join Date
    Oct 2010
    Location
    USA
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Melbourne
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Enthusiast hostentic's Avatar
    Join Date
    Oct 2010
    Location
    USA
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #8
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Melbourne
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hostentic View Post
    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. #9
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Melbourne
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    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.

    Please reply. Thank you

  10. #10
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #11
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Melbourne
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    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.

    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

  12. #12
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,098
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    What I lack in acuracy I make up for in misteaks

  13. #13
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    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


Bookmarks

Posting Permissions

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