0

Hi there I have this PHP script that generates an XML sitemap file. Google does not allow sitemaps to have more than 50,000 records (i.e. URLs) - however I have over a million. I was thinking of putting in a loop but need to figure out how I save the XML output generated by this script to its own sitemap xml file eg. sitemap0_to_50000.xml

Thanks! Any help for this rookie PHP coder would be greatly appreciated :)

<?php
      $databaseServer = "localhost";
      $databaseUsername = "username";
      $databasePassword = "password";
      $databaseName = "database";
      $databaseTable = "tablename";
      header("Content-Type: text/xml");
      function xmlentities($text)
      {
        $search = array('&','<','>','"','\'');
        $replace = array('&amp;','&lt;','&gt;','&quot;','&apos;');
        return str_replace($search,$replace,$text);   
      }
      print chr(60)."?xml version='1.0' encoding='UTF-8'?".chr(62);
      print chr(60)."urlset xmlns='http://www.google.com/schemas/sitemap/0.84' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd'".chr(62);
      $sql = "SELECT * FROM `".$databaseTable."`";
      $link = @mysql_connect($databaseServer,$databaseUsername,$databasePassword);
      @mysql_select_db($databaseName,$link);
      $result = mysql_unbuffered_query($sql,$link);
      while($row = mysql_fetch_array($result,MYSQL_ASSOC))
      {
        // create the loc (URL) value based on the $row array, for example:
        $loc = $row["url"];
        print "<url>";
        print "<loc>".xmlentities($loc)."</loc>";
        print "</url>";
      }
      print "</urlset>";
    ?>

1 Answer 1

1

Instead of echoing it onto a page you could directly write it to an Xml file in PHP

or also you can use the saveXML() function in php to save your complete DOM tree into a file

you can find the complete documentation here

http://php.net/manual/en/domdocument.savexml.php

Hope it helps !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.