将输出保存在xml文件中

时间:2013-06-04 07:07:47

标签: php xml

如何将以下链接的Web内容保存到xml文件中?下面的代码对我不起作用。

<?php
$url = "https://services.boatwizard.com/bridge/events/ae0324ff-e1a5-4a77-9783-f41248bfa975/boats?status=on";
copy($url, "file.xml");
?>

4 个答案:

答案 0 :(得分:2)

您可以通过Curl:

将给定链接的内容下载到file.xml
<?php
$url = 'https://services.boatwizard.com/bridge/events/ae0324ff-e1a5-4a77-9783-f41248bfa975/boats?status=on';
$fp = fopen (dirname(__FILE__) . '/file.xml', 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

答案 1 :(得分:1)

http://de2.php.net/manual/en/book.dom.php

$dom = new DOMDocument();
$dom->load('http://www.example.com');
$dom->save('filename.xml');

答案 2 :(得分:0)

可以按照以下方式完成:

function savefile($filename,$data){
$fh = fopen($filename, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}

$data = file_get_contents('your link');
savefile('file.xml',$data);

答案 3 :(得分:0)

使用2个函数file_get_contents()file_put_contents();

file_get_contents - 获取文件内容,如果已启用fopen包装器,则URL可用作此函数的文件名。

file_put_contents - 将第二个参数的内容放入第一个参数中指定的文件

 <?php
  $url = "https://services.boatwizard.com/bridge/events/ae0324ff-e1a5-4a77-9783-f41248bfa975/boats?status=on";
  file_put_contents('file.xml',file_get_contents($url));
  @chmod('file.xml', 0755); 
?>