我有多个URL,每个URL都指向一个XML文件,尽管手动指向URL需要花费几分钟时间,并等待每个XML被收集并保存到桌面。
因此,我正在寻找一个脚本,可以自动访问URL的(一个接一个)在晚上,并下载每个XML到我们的Apache服务器上的文件夹。
所以我认为需要以下两个代码:1. PHP脚本访问多个定义的URL;2.
我在网上似乎找不到任何相关的东西,所以我的希望寄托在你身上。我想提前感谢您的努力和时间。亲切的问候,理查德。
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");
我希望这对你有帮助。
警告:我是凭脑子写的,没有经过测试。
如果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