<?php
/**
* XML Sitemap to HTML Converter
* Converts an XML sitemap into a formatted HTML page with clickable links
*/
// Configuration
$sitemapUrl = ”; // Set your sitemap URL here, or use the form below
// Handle form submission
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’ && !empty($_POST[‘sitemap_url’])) {
$sitemapUrl = filter_var($_POST[‘sitemap_url’], FILTER_SANITIZE_URL);
}
// Function to fetch and parse the sitemap
function parseSitemap($url) {
// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$xmlContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200 || !$xmlContent) {
return [‘error’ => ‘Failed to fetch sitemap. HTTP Code: ‘ . $httpCode];
}
// Parse XML
libxml_use_internal_errors(true);
$xml = simplexml_load_string($xmlContent);
if ($xml === false) {
$errors = libxml_get_errors();
libxml_clear_errors();
return [‘error’ => ‘Failed to parse XML: ‘ . $errors[0]->message];
}
// Register namespace
$xml->registerXPathNamespace(‘sm’, ‘http://www.sitemaps.org/schemas/sitemap/0.9’);
// Extract URLs
$urls = [];
foreach ($xml->xpath(‘//sm:url’) as $urlNode) {
$url = [
‘loc’ => (string)$urlNode->loc,
‘lastmod’ => isset($urlNode->lastmod) ? (string)$urlNode->lastmod : ”,
‘changefreq’ => isset($urlNode->changefreq) ? (string)$urlNode->changefreq : ”,
‘priority’ => isset($urlNode->priority) ? (string)$urlNode->priority : ”
];
$urls[] = $url;
}
return [‘urls’ => $urls, ‘total’ => count($urls)];
}
// Process sitemap if URL is provided
$result = null;
if (!empty($sitemapUrl)) {
$result = parseSitemap($sitemapUrl);
}
?>
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>XML Sitemap to HTML Converter</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, ‘Segoe UI’, Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
color: #333;
background: #f5f5f5;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #2c3e50;
margin-bottom: 10px;
}
.subtitle {
color: #7f8c8d;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #2c3e50;
}
input[type=”text”] {
width: 100%;
padding: 12px;
border: 2px solid #ddd;
border-radius: 4px;
font-size: 16px;
transition: border-color 0.3s;
}
input[type=”text”]:focus {
outline: none;
border-color: #3498db;
}
button {
background: #3498db;
color: white;
padding: 12px 30px;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #2980b9;
}
.error {
background: #e74c3c;
color: white;
padding: 15px;
border-radius: 4px;
margin: 20px 0;
}
.success {
background: #2ecc71;
color: white;
padding: 15px;
border-radius: 4px;
margin: 20px 0;
}
.sitemap-table {
width: 100%;
margin-top: 30px;
border-collapse: collapse;
overflow-x: auto;
}
.sitemap-table th {
background: #34495e;
color: white;
padding: 12px;
text-align: left;
font-weight: 600;
}
.sitemap-table td {
padding: 12px;
border-bottom: 1px solid #ecf0f1;
}
.sitemap-table tr:hover {
background: #f8f9fa;
}
.sitemap-table a {
color: #3498db;
text-decoration: none;
word-break: break-all;
}
.sitemap-table a:hover {
text-decoration: underline;
}
.stats {
background: #ecf0f1;
padding: 15px;
border-radius: 4px;
margin: 20px 0;
}
.stats strong {
color: #2c3e50;
}
</style>
</head>
<body>
<div class=”container”>
<h1>🗺️ XML Sitemap to HTML Converter</h1>
<p class=”subtitle”>Convert any XML sitemap into a readable HTML format</p>
<form method=”POST” action=””>
<div class=”form-group”>
<label for=”sitemap_url”>Enter Sitemap URL:</label>
<input
type=”text”
id=”sitemap_url”
name=”sitemap_url”
placeholder=”https://example.com/sitemap.xml”
value=”<?php echo htmlspecialchars($sitemapUrl); ?>”
required
>
</div>
<button type=”submit”>Convert Sitemap</button>
</form>
<?php if ($result !== null): ?>
<?php if (isset($result[‘error’])): ?>
<div class=”error”>
<strong>Error:</strong> <?php echo htmlspecialchars($result[‘error’]); ?>
</div>
<?php else: ?>
<div class=”success”>
✓ Successfully parsed sitemap!
</div>
<div class=”stats”>
<strong>Total URLs:</strong> <?php echo $result[‘total’]; ?>
</div>
<table class=”sitemap-table”>
<thead>
<tr>
<th>#</th>
<th>URL</th>
<th>Last Modified</th>
<th>Change Freq</th>
<th>Priority</th>
</tr>
</thead>
<tbody>
<?php foreach ($result[‘urls’] as $index => $url): ?>
<tr>
<td><?php echo $index + 1; ?></td>
<td>
<a href=”<?php echo htmlspecialchars($url[‘loc’]); ?>” target=”_blank”>
<?php echo htmlspecialchars($url[‘loc’]); ?>
</a>
</td>
<td><?php echo htmlspecialchars($url[‘lastmod’]); ?></td>
<td><?php echo htmlspecialchars($url[‘changefreq’]); ?></td>
<td><?php echo htmlspecialchars($url[‘priority’]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php endif; ?>
</div>
</body>
</html>
https://claude.ai/public/artifacts/34ddfc21-ed36-46f1-a4fa-72e75e00f00c
Leave a Reply