Link to home
Start Free TrialLog in
Avatar of jello32
jello32Flag for United States of America

asked on

write contents of output to xml file and post to a website

I have the following code:
<?
  echo '<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  $filenamein = "urls4.txt";
  $urlfile =  @fopen($filenamein, "r") or die("Couldn't open url.txt");
  while (!feof($urlfile)) {
   $line = fgets($urlfile);
   echo '   <url> 
        <loc>' . $line ; 
		echo '</loc>
        <lastmod>2013-06-06</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>';
	
  }
	?>

Open in new window


I want to save the output to a an xml file and then post it to a website.  How do I do this?
Avatar of TvMpt
TvMpt
Flag of Portugal image

$fp = fopen('urls4.txt', 'r');

$xml = new SimpleXMLElement('<allurls></allurls>');

while ($line = fgetcsv($fp)) {
   $node = $xml->addChild('url');
   $node->addChild('loc', $line[0]);
   $node->addChild('lastmod', $line[1]);
   $node->addChild('changefreq', $line[2]);
   $node->addChild('priority', $line[3]);
}

echo $xml->saveXML();

Open in new window


I assume that txt file have values like:
loc1,2013-12-12,daily,0.8
loc2,2012-12-11,daily,1
Avatar of jello32

ASKER

No the txt file has urls in the form of
http://www.example.com/item1.svg
http://www.example.com/item2.svg
but where is the aditional info? like lastmod, changefreq and others?
Avatar of jello32

ASKER

it gets created in my original code. see above.  it's all the same for each url.  last mod date is 6/6/2013, change frequency is daily.
...
while ($line = fgetcsv($fp)) {
   $node = $xml->addChild('url');
   $node->addChild('loc', $line[0]);
   $node->addChild('lastmod', 6/6/2013);
   $node->addChild('changefreq', 'daily');
   $node->addChild('priority', '0.8');
}

...

Open in new window

Avatar of jello32

ASKER

how do I post it to a url?
Avatar of jello32

ASKER

also how do i specify the filename i want it saved as?
instead echo use

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $xml->saveXML());
fclose($fh);
Avatar of jello32

ASKER

I tried what you suggested but it puts 0.000496770988574 as the date for lastmod.  Also how would I put line breaks in as in my original code?  And how to I post it to a website?
ASKER CERTIFIED SOLUTION
Avatar of TvMpt
TvMpt
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might want to consider posting a few different questions here at EE.  It looks like you have several moving parts that need consideration.
puts 0.000496770988574 as the date for lastmod
When you're using DATETIME values, the ISO-8601 standard should be respected for internal representations of dates and also during data interchange.  This article explains how it works.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

New line characters in XML documents can be produced by the PHP_EOL constant.  It is predefined and context-aware, so it will work correctly no matter what OS you're using.  You can use string concatenation to add it to the end of each line of XML.  And, of course, XML does not care about whitespace between tags, so you could just as well omit it.

"How do I post to a website?" is a bit larger question.  In client/server (web) applications, "post" is a term of art, and it seems out of place in your question here.  I don't know that it is out of place, it just seems that way.  You might want to add a new question to EE giving us an example of what you want to do with the XML file.

If you're new to PHP, this book is an excellent learning resource.
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0321833899

Best regards, ~Ray
Avatar of jello32

ASKER

thanks!