Hello,
I have this code:
<?php
function download($file_source, $file_target) {
$rh = fopen($file_source, 'rb');
$wh = fopen($file_target, 'wb');
if ($rh===false || $wh===false) {
// error reading or opening file
return true;
}
while (!feof($rh)) {
if (fwrite($wh, fread($rh, 1024)) === FALSE) {
// 'Download error: Cannot write to file ('.$file_target.')';
return true;
}
}
fclose($rh);
fclose($wh);
// No error
return false;
}
//AW feeds
download('url_of.xml','name_of.xml');
?>
How can i add multiple download urls but only one name to the xml? I mean the name_of.xml should grab data from 1.xml, 2.xml ... N.xml. What sould i modify to the code above?
Thanks,
Gabriel
Hi Gabriel,
It's not totally straight forward to append one XML file to another, however if the XML is not too complicated then the following might work. This involves creating a top level XML element , downloading and appending the remote files to this file, and then closing the top level element. Try something like this:
<?php
function download($file_source, $file_target) {
$rh = fopen($file_source, 'rb');
$wh = fopen($file_target, 'ab');
if ($rh===false || $wh===false) {
// error reading or opening file
return true;
}
while (!feof($rh)) {
if (fwrite($wh, fread($rh, 1024)) === FALSE) {
// 'Download error: Cannot write to file ('.$file_target.')';
return true;
}
}
fclose($rh);
fclose($wh);
// No error
return false;
}
$filename = "name_of.xml";
// create the file and top level XML element
$fh = fopen($filename,"wb");
fwrite($fh,"<myxml>");
fclose($fh);
download('url_of_a.xml',$filename);
download('url_of_b.xml',$filename);
download('url_of_c.xml',$filename);
// end the file by closing the top level XML element
$fh = fopen($filename,"ab");
fwrite($fh,"</myxml>");
fclose($fh);
?>
Hope this helps!
Cheers,
David.