-6

How to save the web content of the below link into an xml file? Below code is not working for me.

<?php
$url = "https://services.boatwizard.com/bridge/events/ae0324ff-e1a5-4a77-9783-f41248bfa975/boats?status=on";
copy($url, "file.xml");
?>
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • [How to get useful error messages in PHP?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – hakre Jun 04 '13 at 07:29

4 Answers4

2

You can download the contents of the given link to file.xml via Curl:

<?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);
?>
Kenny
  • 5,350
  • 7
  • 29
  • 43
1

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

$dom = new DOMDocument();
$dom->load('http://www.example.com');
$dom->save('filename.xml');
IdemeNaHavaj
  • 2,297
  • 1
  • 12
  • 10
0

It can be done as follows:

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);
mrida
  • 1,157
  • 1
  • 10
  • 16
0

Use 2 functions file_get_contents() and file_put_contents();

file_get_contents - gets the content of file, A URL can be used as a filename with this function if the fopen wrappers have been enabled.

file_put_contents - put the contents of second argument to a file specified in first argument

 <?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); 
?>
Robert
  • 19,800
  • 5
  • 55
  • 85
  • There are so many security considerations to account for here. At the very least, please `chmod` file.xml so it's not world-executable. – Zach Rattner Jun 04 '13 at 07:17