【问题标题】:Daily XML's download to webserver from fixed external URL's每日 XML 从固定的外部 URL 下载到 Web 服务器
【发布时间】:2014-06-03 03:37:42
【问题描述】:

我有多个 URL,每个 URL 都指向一个 XML 文件,尽管手动指向 URL 并等待收集每个 XML 并将其保存到桌面需要几分钟。

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

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

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

亲切的问候, 理查德

【问题讨论】:

    标签: php xml cron


    【解决方案1】:
    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");
    

    我希望这会有所帮助..

    警告:我是凭心意写的,未经测试。

    【讨论】:

      【解决方案2】:

      如果 XML 脚本不需要身份验证,您可以使用简单的 shell 脚本和 wget 或 curl 来下载文件

      wget -O /local/path/myfile.xml http://example.com/myfile.xml
      

      curl -o /local/path/myfile.xml http://example.com/myfile.xml
      

      【讨论】:

        猜你喜欢
        • 2020-02-28
        • 1970-01-01
        • 2010-09-15
        • 2011-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多