Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Cek apakah form sudah dikirim
- $urlInput = $_POST['url'] ?? '';
- $data = [];
- $error = '';
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($urlInput)) {
- $url = trim($urlInput);
- // Ambil konten sitemap via cURL
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $xmlContent = curl_exec($ch);
- curl_close($ch);
- if ($xmlContent) {
- libxml_use_internal_errors(true);
- $xml = simplexml_load_string($xmlContent);
- if ($xml !== false) {
- $namespaces = $xml->getNamespaces(true);
- if (isset($namespaces[''])) {
- $xml->registerXPathNamespace('ns', $namespaces['']);
- $urls = $xml->xpath('//ns:url');
- } else {
- $urls = $xml->url;
- }
- // Konversi ke array dan sorting
- foreach ($urls as $url) {
- $loc = (string)$url->loc;
- $lastmod = (string)$url->lastmod;
- $data[] = [
- 'loc' => $loc,
- 'lastmod' => $lastmod,
- 'timestamp' => strtotime($lastmod),
- ];
- }
- usort($data, function($a, $b) {
- return $b['timestamp'] - $a['timestamp'];
- });
- } else {
- $error = "Gagal memproses XML.";
- }
- } else {
- $error = "Gagal mengambil data dari URL.";
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="id">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Reverse Sitemap by Lastmod</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- padding: 1rem;
- margin: 0;
- background: #f5f5f5;
- }
- .container {
- max-width: 800px;
- margin: auto;
- background: white;
- padding: 2rem;
- border-radius: 8px;
- box-shadow: 0 4px 10px rgba(0,0,0,0.1);
- }
- h1 {
- text-align: center;
- font-size: 1.8rem;
- margin-bottom: 1rem;
- }
- form {
- display: flex;
- flex-direction: column;
- gap: 1rem;
- }
- input[type="text"] {
- padding: 0.75rem;
- font-size: 1rem;
- border-radius: 4px;
- border: 1px solid #ccc;
- }
- input[type="submit"] {
- padding: 0.75rem;
- background-color: #007BFF;
- border: none;
- color: white;
- font-weight: bold;
- font-size: 1rem;
- border-radius: 4px;
- cursor: pointer;
- }
- input[type="submit"]:hover {
- background-color: #0056b3;
- }
- .error {
- color: red;
- font-weight: bold;
- }
- .results {
- margin-top: 2rem;
- }
- .url-entry {
- margin-bottom: 0.75rem;
- }
- a {
- color: #007BFF;
- text-decoration: none;
- word-wrap: break-word;
- }
- a:hover {
- text-decoration: underline;
- }
- .timestamp {
- display: block;
- color: #555;
- font-size: 0.9rem;
- }
- @media (max-width: 600px) {
- input[type="text"], input[type="submit"] {
- width: 100%;
- }
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>Urutkan Sitemap Berdasarkan Lastmod</h1>
- <form method="POST">
- <input type="text" name="url" placeholder="Masukkan URL sitemap.xml" value="<?= htmlspecialchars($urlInput) ?>" required>
- <input type="submit" value="Ambil & Urutkan">
- </form>
- <?php if (!empty($error)): ?>
- <p class="error"><?= htmlspecialchars($error) ?></p>
- <?php endif; ?>
- <?php if (!empty($data)): ?>
- <div class="results">
- <h2>Hasil Terurut:</h2>
- <?php foreach ($data as $entry): ?>
- <div class="url-entry">
- <a href="<?= htmlspecialchars($entry['loc']) ?>" target="_blank">
- <?= htmlspecialchars($entry['loc']) ?>
- </a>
- <span class="timestamp">Last modified: <?= htmlspecialchars($entry['lastmod']) ?></span>
- </div>
- <?php endforeach; ?>
- </div>
- <?php endif; ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment