ssaidz

curl sitemap reorder

May 29th, 2025
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.66 KB | Source Code | 0 0
  1. <?php
  2. // Cek apakah form sudah dikirim
  3. $urlInput = $_POST['url'] ?? '';
  4. $data = [];
  5. $error = '';
  6.  
  7. if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($urlInput)) {
  8.     $url = trim($urlInput);
  9.  
  10.     // Ambil konten sitemap via cURL
  11.     $ch = curl_init();
  12.     curl_setopt($ch, CURLOPT_URL, $url);
  13.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14.     $xmlContent = curl_exec($ch);
  15.     curl_close($ch);
  16.  
  17.     if ($xmlContent) {
  18.         libxml_use_internal_errors(true);
  19.         $xml = simplexml_load_string($xmlContent);
  20.         if ($xml !== false) {
  21.             $namespaces = $xml->getNamespaces(true);
  22.             if (isset($namespaces[''])) {
  23.                 $xml->registerXPathNamespace('ns', $namespaces['']);
  24.                 $urls = $xml->xpath('//ns:url');
  25.             } else {
  26.                 $urls = $xml->url;
  27.             }
  28.  
  29.             // Konversi ke array dan sorting
  30.             foreach ($urls as $url) {
  31.                 $loc = (string)$url->loc;
  32.                 $lastmod = (string)$url->lastmod;
  33.                 $data[] = [
  34.                     'loc' => $loc,
  35.                     'lastmod' => $lastmod,
  36.                     'timestamp' => strtotime($lastmod),
  37.                 ];
  38.             }
  39.  
  40.             usort($data, function($a, $b) {
  41.                 return $b['timestamp'] - $a['timestamp'];
  42.             });
  43.         } else {
  44.             $error = "Gagal memproses XML.";
  45.         }
  46.     } else {
  47.         $error = "Gagal mengambil data dari URL.";
  48.     }
  49. }
  50. ?>
  51.  
  52. <!DOCTYPE html>
  53. <html lang="id">
  54. <head>
  55.     <meta charset="UTF-8">
  56.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  57.     <title>Reverse Sitemap by Lastmod</title>
  58.     <style>
  59.         body {
  60.             font-family: Arial, sans-serif;
  61.             padding: 1rem;
  62.             margin: 0;
  63.             background: #f5f5f5;
  64.         }
  65.  
  66.         .container {
  67.             max-width: 800px;
  68.             margin: auto;
  69.             background: white;
  70.             padding: 2rem;
  71.             border-radius: 8px;
  72.             box-shadow: 0 4px 10px rgba(0,0,0,0.1);
  73.         }
  74.  
  75.         h1 {
  76.             text-align: center;
  77.             font-size: 1.8rem;
  78.             margin-bottom: 1rem;
  79.         }
  80.  
  81.         form {
  82.             display: flex;
  83.             flex-direction: column;
  84.             gap: 1rem;
  85.         }
  86.  
  87.         input[type="text"] {
  88.             padding: 0.75rem;
  89.             font-size: 1rem;
  90.             border-radius: 4px;
  91.             border: 1px solid #ccc;
  92.         }
  93.  
  94.         input[type="submit"] {
  95.             padding: 0.75rem;
  96.             background-color: #007BFF;
  97.             border: none;
  98.             color: white;
  99.             font-weight: bold;
  100.             font-size: 1rem;
  101.             border-radius: 4px;
  102.             cursor: pointer;
  103.         }
  104.  
  105.         input[type="submit"]:hover {
  106.             background-color: #0056b3;
  107.         }
  108.  
  109.         .error {
  110.             color: red;
  111.             font-weight: bold;
  112.         }
  113.  
  114.         .results {
  115.             margin-top: 2rem;
  116.         }
  117.  
  118.         .url-entry {
  119.             margin-bottom: 0.75rem;
  120.         }
  121.  
  122.         a {
  123.             color: #007BFF;
  124.             text-decoration: none;
  125.             word-wrap: break-word;
  126.         }
  127.  
  128.         a:hover {
  129.             text-decoration: underline;
  130.         }
  131.  
  132.         .timestamp {
  133.             display: block;
  134.             color: #555;
  135.             font-size: 0.9rem;
  136.         }
  137.  
  138.         @media (max-width: 600px) {
  139.             input[type="text"], input[type="submit"] {
  140.                 width: 100%;
  141.             }
  142.         }
  143.     </style>
  144. </head>
  145. <body>
  146.     <div class="container">
  147.         <h1>Urutkan Sitemap Berdasarkan Lastmod</h1>
  148.         <form method="POST">
  149.             <input type="text" name="url" placeholder="Masukkan URL sitemap.xml" value="<?= htmlspecialchars($urlInput) ?>" required>
  150.             <input type="submit" value="Ambil & Urutkan">
  151.         </form>
  152.  
  153.         <?php if (!empty($error)): ?>
  154.             <p class="error"><?= htmlspecialchars($error) ?></p>
  155.         <?php endif; ?>
  156.  
  157.         <?php if (!empty($data)): ?>
  158.             <div class="results">
  159.                 <h2>Hasil Terurut:</h2>
  160.                 <?php foreach ($data as $entry): ?>
  161.                     <div class="url-entry">
  162.                         <a href="<?= htmlspecialchars($entry['loc']) ?>" target="_blank">
  163.                             <?= htmlspecialchars($entry['loc']) ?>
  164.                         </a>
  165.                         <span class="timestamp">Last modified: <?= htmlspecialchars($entry['lastmod']) ?></span>
  166.                     </div>
  167.                 <?php endforeach; ?>
  168.             </div>
  169.         <?php endif; ?>
  170.     </div>
  171. </body>
  172. </html>
  173.  
Advertisement
Add Comment
Please, Sign In to add comment