2015-05-14 57 views

回答

2

使用的file_get_contents東西:

$robotsContents = file_get_contents("http://targetdomain.com/robots.txt"); 
$sitemapContents = file_get_contents("http://targetdomain.com/sitemap.xml"); 

檢查的內容是假的,假的就意味着404沒找到,然後檢查,如果它不是HTML內容(因爲有些站點重定向每個URL)與strpos($robotsContents, '<html') === false,如果沒有標籤,則表示它可以是txt或xml文件。

所以:

function pathExistsAndIsNotHtml($path) { 
    $contents = @file_get_contents($path); 
    return ! empty($contents) && strpos($contents, '<html') === false; 
} 
if(pathExistsAndIsNotHtml("http://targetdomain.com/robots.txt")) { 
    echo '<a href="http://targetdomain.com/robots.txt">http://targetdomain.com/robots.txt</a>'; 
} else { 
    echo 'There is no robots.txt'; 
} 
if(pathExistsAndIsNotHtml("http://targetdomain.com/sitemap.xml")) { 
    echo '<a href="http://targetdomain.com/sitemap.xml">http://targetdomain.com/sitemap.xml</a>'; 
} else { 
    echo 'There is no sitemap.xml'; 
} 
+0

謝謝你,它的工作,在這裏我不想顯示我只是想閱讀的內容,內容並顯示http://targetdomain.com/robots.txt鏈接。怎麼做? – user1992

+0

謝謝..現在我想下載它的pdf格式 – user1992

+1

這不是Stackoverflow的目的,你不會在這裏得到你的完整代碼。我們在帖子上回答了一個有問題的問題。如果安裝是正確的,請驗證它,並在另一篇文章中給出您的代碼以及阻止您的代碼。 – KyleK