如何将以下链接的网页内容保存到xml文件中?下面的代码不适合我。将输出保存在xml文件中
<?php
$url = "https://services.boatwizard.com/bridge/events/ae0324ff-e1a5-4a77-9783-f41248bfa975/boats?status=on";
copy($url, "file.xml");
?>
如何将以下链接的网页内容保存到xml文件中?下面的代码不适合我。将输出保存在xml文件中
<?php
$url = "https://services.boatwizard.com/bridge/events/ae0324ff-e1a5-4a77-9783-f41248bfa975/boats?status=on";
copy($url, "file.xml");
?>
这是可以做到如下:
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);
谢谢!!但得到一个错误,因为无法打开文件.. – user2334667
这是因为你没有权限写入当前目录。 – mrida
使用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);
?>
这里有很多安全考虑。至少,请'chmod' file.xml,这样它不是世界可执行的。 –
http://de2.php.net/manual/en/book.dom.php
$dom = new DOMDocument();
$dom->load('http://www.example.com');
$dom->save('filename.xml');
您也可以下载给定链路通过到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);
?>
[如何获得有用的er ror消息在PHP?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – hakre