Skip to content

Instantly share code, notes, and snippets.

@nikillpop
Forked from artlung/sitemap.column.xml.php
Created September 9, 2019 23:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nikillpop/02858663e9863a39db144cc07be4ede8 to your computer and use it in GitHub Desktop.
This is a quick way to turn a simple comma-separated file with a list of urls in each column representing one sitemap into a valid XML Sitemap:
<?php
/**
* User: artlung
* Date: 2019-08-24
* A variation of https://gist.github.com/artlung/210438 :
*
* "I have a list of URLs in 10 columns of a CSV file and I need a separate
* sitemap for each column. Can we modify this code accordingly?"
*
* This is a quick way to turn a simple comma-separated file
* with a list of urls in each column representing one sitemap (urls-in-columns.csv)
* into a valid XML Sitemap:
* http://en.wikipedia.org/wiki/Sitemaps
* Put this file sitemap.column.xml.php and urls-in-columns.csv at
* the webroot http://example.com/sitemap.column.xml.php
* Then add the text in quotes below to your robots.txt file as new lines:
* "Sitemap: http://example.com/sitemap.column.xml.php?n=0"
* "Sitemap: http://example.com/sitemap.column.xml.php?n=1"
* "Sitemap: http://example.com/sitemap.column.xml.php?n=2"
* "Sitemap: http://example.com/sitemap.column.xml.php?n=3"
* ...and so on.
* Questions? email joe@artlung.com
*/
$filename = 'urls-in-columns.csv';
$filectime = filectime($filename);
$handle = fopen($filename, 'r');
$urls = fgetcsv($handle);
// First pass: how many columns in this csv?
$number_of_columns = count($urls);
$max_index = $number_of_columns - 1;
fclose($handle);
$requested_column_index = isset($_GET['n']) ? intval($_GET['n']) : 0;
if ($requested_column_index > $max_index) {
$requested_column_index = 0;
}
// Second pass, read the csv for the data we need for the sitemap
$handle = fopen($filename, 'r');
while (($data = fgetcsv($handle)) !== FALSE) {
$priority = '0.5';
$url = trim($data[$requested_column_index]);
if ($url) {
$sitemap[] = array(
'loc' => $url,
'lastmod' => date('Y-m-d',$filectime),
'changefreq' => 'weekly',
'priority' => $priority,
);
}
}
fclose($handle);
header('Content-Type: text/xml');
echo '<?xml version=\'1.0\' encoding=\'UTF-8\'?>';
echo "\n";
echo '<!-- showing column index ' . $requested_column_index . " min n=0, max n={$max_index}; {$number_of_columns} columns". ' -->';
echo "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
echo "\n";
foreach ($sitemap as $link) {
echo "\t<url>\n";
echo "\t\t<loc>" . htmlentities($link['loc']) . "</loc>\n";
echo "\t\t<lastmod>{$link['lastmod']}</lastmod>\n";
echo "\t\t<changefreq>{$link['changefreq']}</changefreq>\n";
echo "\t\t<priority>{$link['priority']}</priority>\n";
echo "\t</url>\n";
}
echo '</urlset>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment