Tags
Asked 2 years ago
30 Jun 2021
Views 261
Weldon

Weldon posted

Creating an XML sitemap with PHP

Creating an XML sitemap with PHP
sec8

sec8
answered Apr 28 '23 00:00

To create an XML sitemap with PHP, you can follow these steps:

Start by creating a new XML document with the <?xml version="1.0" encoding=" UTF-8"?> declaration, and add the <urlset> element with the appropriate namespace declaration. For example:


$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');

Connect to your database or retrieve your website's URLs from your file system using PHP. For example, you could use a database query to retrieve all the URLs from your website's database, like this:


$host = 'localhost';
$username = 'db_user';
$password = 'db_password';
$dbname = 'my_database';

// Create a new PDO instance
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
$pdo = new PDO($dsn, $username, $password);

// Retrieve all URLs from the database
$stmt = $pdo->query("SELECT url, lastmod FROM pages");
$urls = $stmt->fetchAll(PDO::FETCH_ASSOC);

Loop through the array of URLs and add each URL to the XML document as a <url> element with child elements for the URL's location, last modified date, change frequency, and priority (if desired). For example:

f

oreach ($urls as $url) {
    $urlElement = $xml->addChild('url');
    $urlElement->addChild('loc', htmlspecialchars($url['url']));
    $urlElement->addChild('lastmod', htmlspecialchars($url['lastmod']));
    $urlElement->addChild('changefreq', 'monthly');
    $urlElement->addChild('priority', '0.5');
}

Save the XML document to a file with a .xml extension using the asXML() method. For example:


$xml->asXML('sitemap.xml');

The XML sitemap is now ready and can be submitted to search engines via their webmaster tools or other submission methods.
Here is the complete code for creating an XML sitemap with PHP using the SimpleXMLElement class:



<?php
// Create a new XML document
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');

// Connect to the database and retrieve all URLs
$host = 'localhost';
$username = 'db_user';
$password = 'db_password';
$dbname = 'my_database';

$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
$pdo = new PDO($dsn, $username, $password);
$stmt = $pdo->query("SELECT url, lastmod FROM pages");
$urls = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Loop through the URLs and add them to the XML document
foreach ($urls as $url) {
    $urlElement = $xml->addChild('url');
    $urlElement->addChild('loc', htmlspecialchars($url['url']));
    $urlElement->addChild('lastmod', htmlspecialchars($url['lastmod']));
    $urlElement->addChild('changefreq', 'monthly');
    $urlElement->addChild('priority', '0.5');
}

// Save the XML document to a file
$xml->asXML('sitemap.xml');
?>

This code will create a sitemap.xml file in the same directory as the PHP script, containing URLs for all the pages in the database.
Post Answer