2017-02-22 34 views
2

這是我在文件sitemap.php我的動態地圖代碼,但我想,因爲谷歌的打印或顯示在「sitemap.xml的」這整個頁面的數據總是會考慮sitemap.This sitemap.xml的網頁工作正常,但我想要所有的數據在sitemap.xml。 如何做到這一點?如何在sitemap.xml上打印或顯示sitemap.php數據?

<?php 
header("Content-type: text/xml"); 
echo '<?xml version="1.0" encoding="UTF-8" ?>'; 
include 'includes/connectdb.php'; 
?> 

<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd"> 

    <url> 
     <loc>http://www.mobilesapp.com/</loc> 
     <priority>1.00</priority> 
    </url> 

<?php 
    $stmt = $con->prepare("SELECT url,modified FROM localnews"); 
    $stmt->execute(); 
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 
    foreach($rows as $row){ 
?> 
<url> 
    <loc>http://www.mobilesapp.com/<?= $row['url'] ;?></loc> 
    <lastmod><?= $row['modified']; ?></lastmod> 
    <changefreq>always</changefreq> 
    <priority>1</priority> 
</url> 
<?php } ?> 

<?php 
    $stmt = $con->prepare("SELECT url,modified FROM socialnews"); 
    $stmt->execute(); 
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 
    foreach($rows as $row){ 
?> 
<url> 
    <loc>http://www.mobilesapp.com/<?= $row['url'] ;?></loc> 
    <lastmod><?= $row['modified']; ?></lastmod> 
    <changefreq>always</changefreq> 
    <priority>1</priority> 
</url> 
<?php } ?> 

</urlset> 
+0

在文件 標題的開始嘗試這個代碼( '內容 - 類型:文本/ XML'); header('Content-Disposition:attachment; filename =「sitemap.xml」'); – user247217

+0

您需要重寫規則。我只是刪除PHP文件的內容,因爲它工作正常,並且您需要的唯一東西就是顯示爲不同URL的響應。 –

+0

@ user247217親愛的,你的代碼只能將我的php文件下載到xml文件中。它不會重寫我已經存在的文件** sitemap.xml ** –

回答

1
<?php 
    $xml = '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">'; 
    // mysql select 
    foreach ($rows as $row) { 
     // add next part to xml 
     $xml .= ' 
     <url> 
      <loc>http://www.mobilesapp.com/'.$row['url'].'</loc> 
      <lastmod>'.$row['modified'].'</lastmod> 
      <changefreq>always</changefreq> 
      <priority>1</priority> 
     </url> 
     '; 
    } 
    $xml .= '</urlset>'; 
    // show 
    echo $xml; 
    // and save to file 
    file_put_contents('sitemap.xml', $xml); 
    ?> 
+0

file_put_contents('sitemapxml.xml',$ xml); – 2017-02-22 13:41:40