Kuubs
Kuubs

Reputation: 1340

Download external XML file every day

I make use of an external XML file I can get from a certain URL. Now there is a problem regarding the getting of the XML file, because if you try too much times to get the file, you don't get anything at all, presumably to limit the amount of requests.

Is there a possibility to download via PHP the XML file every day to limit the requests to the external server.

I have checked what options I have and I saw that CRON is the most found solution to this problem. But I want to do this via PHP if that is possible, because I don't have the access to the server to setup CRON.

Does anyone have any experience with downloading an XML file to your own server and use that, and download that XML file daily to limit the requests?

I have this code to get the actual XML file:

$xml = file_get_contents("my-xml-file-url-external");
file_put_contents("my-path-to-save-xml-file", $xml);

But how can I make sure this gets called every day?

Upvotes: 1

Views: 351

Answers (1)

Kep
Kep

Reputation: 5857

You can check the last modified time (see filemtime() documentation) of the file you write to, and if it's more than a day old (or non-existant) overwrite it:

$cacheFile = "file.xml";

if (!file_exists($cacheFile) || filemtime($cacheFile) < time() - 86400)
{
    $xml = file_get_contents("my-xml-file-url-external");
    file_put_contents($cacheFile, $xml);
} else {
    $xml = file_get_contents($cacheFile);
}

Upvotes: 3

Related Questions