2017-05-28 122 views
0

我正在尋找一個簡單的腳本生成一個動態地圖,當我遇到我有什麼下面傳來:PHP:創建動態地圖

<?php 
    header("Content-Type: application/xml; charset=utf-8"); 
    echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' .PHP_EOL; 

    function urlElement($url) { 
     echo '<url>'.PHP_EOL; 
     echo '<loc>'.$url.'</loc>'. PHP_EOL; 
     echo '<changefreq>weekly</changefreq>'.PHP_EOL; 
     echo '</url>'.PHP_EOL; 
    } 

    urlElement('https://www.example.com/sub1'); 
    urlElement('https://www.example.com/sub2'); 
    urlElement('https://www.example.com/sub2'); 
    echo '</urlset>'; 
?> 

上面的代碼工作完全生成指定的網址的Sitemap,但我需要的東西可以循環訪問指定的URL並獲取它們上的所有鏈接以創建單個站點地圖,同時忽略重複的鏈接。

+0

將你的URL存儲在一個數組中,然後在數組上做一個'foreach()'並在循環中調用'urlElement($ value);'。 –

+0

嗨,謝謝你的建議,但是我的知識在這方面有限,你能幫助你剛纔建議的嗎? – user2969009

+0

見我的回答如下,感謝 –

回答

0

Store中的數組中的網址,然後做一個foreach()陣列上,並調用urlElement($值);關於循環內的值。

<?php 

    function urlElement($url) { 
     echo '<url>'.PHP_EOL; 
     echo '<loc>'.$url.'</loc>'. PHP_EOL; 
     echo '<changefreq>weekly</changefreq>'.PHP_EOL; 
     echo '</url>'.PHP_EOL; 
    } 

    $urls = array(
     "http://www.google.com", 
     "http://www.google.com/images", 
     "http://www.google.com/maps" 
    ); 



    header("Content-Type: application/xml; charset=utf-8"); 
    echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' .PHP_EOL; 

    foreach($urls as $value) { 
     urlElement($value); 
    } 

    echo '</urlset>'; 
?> 
+0

做到這一點,最好的辦法是所有存儲在數據庫中的鏈接,如果你沒有一個靜態的網站,只要添加一個內部鏈接,你將它添加到數據庫中 - 那麼你讀取數據庫並將信息拖入'$ url'數組中。目前還沒有一個快速解決方案,並且需要在您的網站上進行一些編程才能使其工作。 或者,你可以使用像'的file_get_contents()'和使用正則表達式搜索任何''標籤,並將其存儲在一個數組,但是這是非常糟糕的資源,如果你這樣做的每一頁,每加載站點地圖的時間。 –

+0

以下是我從你的代碼了動態獲取內容:每週每週每週 – user2969009