nddb
Sep 14th 2006, 10:25 am
I don't know if anyone will find this useful, but basically, you pass all your URLs one by one to this function like this :
write_xml("http://www.yoursite.com","1.0");
The 1.0 is the priority level it will be given. If you don't specify one it defaults to 0.5 (just like google says it does.)
It writes to "sitemap1.xml" after 50,000 URLs have been passed to the function, it closes that and initializes "sitemap2.xml" and starts writing URLs there... etc. When you reach the end, you call write_xml("END"); that tells the function that you are done sending URLs, it writes the closing tags to whatever sitemap # it is on, and closes it.
So, if you have a loop that iterates through your database or forum or however you get your list of URLs, you just plug them in here, and it writes sitemap#.xml for you.
It seems to work alright for me, it writes sitemap1.xml -> sitemap10.xml as I cycle through my database making all the URLs.
global $handle, $counter, $file_number, $filename;
$counter = 0;
$file_number = 1;
function write_xml($url,$priority = 0.5)
{
global $handle, $counter, $file_number, $filename;
if($counter == 50000)
{
$file_number++;
$counter = 0;
// Attach end of file, and close it here.
fwrite($handle,"</urlset>\n");
fclose($handle);
}
if($counter == 0)
{
// Open next file here.
$filename = "sitemap" . $file_number . ".xml";
$handle = fopen($filename,"w+");
fwrite($handle,'<?xml version="1.0" encoding="UTF-8"?>' . "\n");
fwrite($handle,'<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">' . "\n");
}
if($url != "" && $url != "END")
{
fwrite($handle," <url>\n");
fwrite($handle," <loc>$url</loc>\n");
fwrite($handle," <priority>$priority</priority>\n");
fwrite($handle," </url>\n");
}
if($url == "END")
{
fwrite($handle,"</urlset>\n");
fclose($handle);
}
// Increment counter for every URL.
$counter++;
}
write_xml("http://www.yoursite.com","1.0");
The 1.0 is the priority level it will be given. If you don't specify one it defaults to 0.5 (just like google says it does.)
It writes to "sitemap1.xml" after 50,000 URLs have been passed to the function, it closes that and initializes "sitemap2.xml" and starts writing URLs there... etc. When you reach the end, you call write_xml("END"); that tells the function that you are done sending URLs, it writes the closing tags to whatever sitemap # it is on, and closes it.
So, if you have a loop that iterates through your database or forum or however you get your list of URLs, you just plug them in here, and it writes sitemap#.xml for you.
It seems to work alright for me, it writes sitemap1.xml -> sitemap10.xml as I cycle through my database making all the URLs.
global $handle, $counter, $file_number, $filename;
$counter = 0;
$file_number = 1;
function write_xml($url,$priority = 0.5)
{
global $handle, $counter, $file_number, $filename;
if($counter == 50000)
{
$file_number++;
$counter = 0;
// Attach end of file, and close it here.
fwrite($handle,"</urlset>\n");
fclose($handle);
}
if($counter == 0)
{
// Open next file here.
$filename = "sitemap" . $file_number . ".xml";
$handle = fopen($filename,"w+");
fwrite($handle,'<?xml version="1.0" encoding="UTF-8"?>' . "\n");
fwrite($handle,'<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">' . "\n");
}
if($url != "" && $url != "END")
{
fwrite($handle," <url>\n");
fwrite($handle," <loc>$url</loc>\n");
fwrite($handle," <priority>$priority</priority>\n");
fwrite($handle," </url>\n");
}
if($url == "END")
{
fwrite($handle,"</urlset>\n");
fclose($handle);
}
// Increment counter for every URL.
$counter++;
}