dongwei4096 2017-05-31 15:46
浏览 68
已采纳

每天下载外部XML文件

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?

  • 写回答

1条回答 默认 最新

  • duanjing3656 2017-05-31 15:51
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题