Jump to content

Trying to create a sitemap generator


l3rodey

Recommended Posts

Hi everyone,

 

This may seem stupid and dumb but what I am trying to do make sense to me. 

I need to create a document .xml sitemap. So it's split into 3 this is very basic.

 

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
  xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
 
is the top
 
<url>
  <loc>' . $URL . '</loc>
  <changefreq>monthly</changefreq>
  <priority>0.50</priority>
</url>
 
is the middle and 
 
</urlset>
 
is the bottom. Makes sense? Top and bottom stay the same I only need one of them and that is it. The middle on the other hand repeats.
 
What I have in PHP is the following: 
 
<?php 
$getURLS = mysql_query("SELECT * FROM urls", $url);
while($row = mysql_fetch_array($getURLS)) {
$URL = $row['url'];
?>
<url>
  <loc>' . $URL . '</loc>
  <changefreq>monthly</changefreq>
  <priority>0.50</priority>
</url>
<?php } ?>
 
That is the script, I am using and it should repeat it... the problem is I need to create it as an xml document.... not a php where I am running it. so what I have is 
 
$fp = fopen("sitemaps/sitemap.xml", "w");
fwrite($fp, $somevariable);
fclose($fp);

 

But yeah I don't know how to piece it together so I run a php document and it outputs all my urls from the database into one document. I literally have them all sitting in the database and just need a document to generate the sitemap.xml... It's failing so hard.

Please someone help me :( 

Link to comment
Share on other sites

Are you only interested in just generating the xml file, not having to manipulate it later? If so then you can more or less do what you're already doing except assign the stuff to a string and then save it to the file instead of outputting it to the screen.

 

It would basically look something like this:

 

<?php

$xmlData = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
  xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
EOF;


// code to connect to the database here

$getURLS = mysql_query("SELECT * FROM urls", $url);
while($row = mysql_fetch_array($getURLS)) {

$URL = $row['url'];

$xmlData.=<<<EOF
<url>
  <loc>{$URL}</loc>
  <changefreq>monthly</changefreq>
  <priority>0.50</priority>
</url>
EOF;

} // end while

$xmlData.="</urlset>";


file_put_contents("sitemaps/sitemap.xml", $xmlData);

?>
I uh, suppose this might have to be modified to write one chunk at a time if this is going to generate a big file, but I've never really seen huge sitemap files before so this should probably be fine.

 

Now.. if you are wanting to be able to manipulate the xml file, you would be better off using something like the SimpleXML class.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.