每天将XML从固定的外部URL下载到Web服务器 [英] Daily XML's download to webserver from fixed external URL's

查看:92
本文介绍了每天将XML从固定的外部URL下载到Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个URL,每个URL都指向一个XML文件,尽管要花几分钟时间手动指向URL,然后等待每种XML收集并保存到桌面。

I have multiple URL's which each points to a XML file, though it takes a few minutes pointing to URL manually and waiting for each XML to be collected and saved to desktop.

因此,我正在寻找一个脚本,该脚本可以在夜间自动访问URL(一个接一个)并将每个XML下载到我们Apache服务器上的文件夹中。

Therefor I am looking for a script that could automatically visit the URL's (one by one) at night and download each XML to a folder on our Apache server.

因此,我认为需要以下两个代码:
1. PHP脚本可访问多个定义的URL;
2. Cron脚本每晚在第1点以上运行。

So I think the following two codes are required: 1. PHP script to visit multiple defined URL's; 2. Cron script to run nightly above point-1.

我似乎在网上找不到任何相关内容,所以希望与您同在。
非常感谢您付出的努力和时间。

I can't seem to find anything relevant on the net, so my hope is with you. I'd like to thank you in advance for taking the effort and time.

亲切的问候,
理查德

Kind regards, Richard

推荐答案

class SimpleCrawler {
    private $url;
    private $data; 
    public function __construct($url){
         $this->url = $url;
         $this->load();
    }

    public function load(){
        $this->data = file_get_contents($this->url);
    }

    public function getData(){
       return $this->data;
    }


}

文件类别:

Class File {
     protected static $instance;
     protected $isResource = false;
     public $item;

     public static function getInstance(){
         if(!self::$instance){
             self::$instance = new self();
         }

         return self::$instance;
     }

     public function setResource($flag){
          $this->isResource = $flag;
     }
     public static function fromFile($path){
         $obj = self::getInstance();
         $obj->item = $path;

         return $obj;
     }

     public static function fromSource($string){
         $obj = self::getInstance();
         $obj->item = $string;
         $obj->setResource(true);

         return $obj;
     }

     public function save($path){

       try {
        if($this->isResource){
            $fopen = fopen($path,'w');

            fwrite($fopen,$this->item);
        }
        else {
             copy($this->item,$path);
        }

       }
       Catch(Exception $e){
          throw $e;
       }
     }
}

以及使用方法:

$getXML = new SimpleCrawler('http://mydomain.com/file.xml');
$xmlString = $getXML->getData();

$file = File::fromSource($xmlString);
$file->save("/my/writtable/path/file.xml");

我希望这会有所帮助。.

I hope this helps..

警告:我是从脑子里写出来的,未经测试。

Warning: I write it from mind, it's not tested.

这篇关于每天将XML从固定的外部URL下载到Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆