Skip to content

Instantly share code, notes, and snippets.

@woody180
Last active January 29, 2023 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woody180/160ccd408cb67aecd0e95d9caac2bc54 to your computer and use it in GitHub Desktop.
Save woody180/160ccd408cb67aecd0e95d9caac2bc54 to your computer and use it in GitHub Desktop.
Google Sitemap Generator
<?php
require_once 'functions.php';
require_once 'rb-mysql.php';
///////////////////////// SETUP /////////////////////////
date_default_timezone_set('Asia/Tbilisi');
$baseurl = 'http://localhost:3000';
$dbuser = 'root';
$dbpassword = '';
$dbname = 'magma';
R::setup( 'mysql:host=localhost;dbname='.$dbname.'',''.$dbuser.'', ''.$dbpassword.'' );
$articles = R::getAll('select url, createdat from articles limit 50');
///////////////////////// WORKING WITH SITEMAP XML /////////////////////////
$xml = new DOMDocument("1.0", "UTF-8");
$xml->formatOutput = true;
//////////////////////////
$urlset = $xml->createElement('urlset');
$xml->appendChild($urlset);
foreach ($articles as $a) {
$article=$xml->createElement("url");
$urlset->appendChild($article);
$url = $xml->createElement('loc', $baseurl . '/' . $a['url']);
$article->appendChild($url);
$timestamp = $xml->createElement('lastmod', date("Y-m-d\TH:i:sP", $a['createdat']));
$article->appendChild($timestamp);
$priority = $xml->createElement('priority', 1);
$article->appendChild($priority);
}
echo "<xmp>".$xml->saveXML()."</xmp>";
if (file_exists(__DIR__ . "/sitemap.xml")) unlink(__DIR__ . "/sitemap.xml");
$xml->save("sitemap.xml");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment