PHP function to generate an XML sitemap

Discussion in 'PHP' started by nddb, Sep 14, 2006.

  1. #1
    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++;
        }
    
    Code (markup):
     
    nddb, Sep 14, 2006 IP
    cesarcesar likes this.
  2. klavixs

    klavixs Guest

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hey thats pretty nifty will defiantly help with better google indexing andrank.i never knew that function i appreciate it
     
    klavixs, Sep 14, 2006 IP
  3. nddb

    nddb Peon

    Messages:
    803
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! Oops.. I updated it, you have to declare the global variables outside the function and set them to their defaults.

    Also, a small example, for putting all the topics from a phpBB forum in the sitemap. You still need to connect to your database, and do all that, but just to show you how you can loop through the database and write out the URLs :

    
    mysql_select_db("phpBB_forums");
    $result = mysql_query("SELECT DISTINCT(topic_id) FROM phpbb_posts");
    while($row = mysql_fetch_array($result))
        {
        $url = "http://www.yoursite.com/forum/viewtopic.php?t=" .$row['topic_id'] . "\n";
        write_xml($url);
        }
    
    Code (markup):
     
    nddb, Sep 14, 2006 IP
  4. doubler

    doubler Active Member

    Messages:
    952
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    70
    #4
    it's fine;)
     
    doubler, Jun 27, 2007 IP
  5. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    awesome! thanks.
     
    cesarcesar, May 25, 2011 IP
  6. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #6
    You do not need the global statement outside of the function though.
     
    DomainerHelper, May 26, 2011 IP