Search code examples
phptimed

save bit of realtime data 4 times per day with php without human interaction


We're building an application that uses data from an api. We don't want to proces all this data because it's too much. We want to capture the data 4 times per day. Therefore we need a php script that saves this to the database without a button or human help.

We know how to get the data and understand how to upload this to a database. We don't know how to do this timed and if this is even possible.

We tried to do this with this code, but the page needs to be refreshed to echo.

if (date('His') == 114300) {
  echo date('His');
}

Solution

  • A PHP script only runs when it's called via a request from a browser or another client, or when run through the command line.

    You could, in theory, create a script with infinite execution time, and have that running. The script would need to check the server time and make the requests. The problem would be that it'd stop if the server restarted, etc. It's also highly inefficient.

    Just call your script with a cronjob, four times a day, e.g. like so:

    0 */6 * * * curl -k http://example.com/download.php >/dev/null 2>&1
    

    The /6 means every six hours. The curl command calls the script and >/dev/null discards the output.