2014-05-13 36 views
0

我試圖創建一個檢查URL是否存在的PHP腳本。 我得到了一個數組中的多個URL我想檢查,但只有當數組$ sources中的第一個URL不存在,我不知道我該如何做到這一點。檢查數組中是否存在多個URL,但僅當數組中的第一個URL不存在

下面的PHP代碼是「我認爲」以及我希望如何實現的,但這僅僅是一個示例,很可能不是正確的做法。

PHP代碼:

<?php 
// an array that contains all the URL sources we can use 
$sources = array("source-1.com/img.png", "source-2.com/img.png", "source-3.com/img.png", "source-4.com/img.png"); 

foreach($sources as $source) { 

$source_headers = @get_headers($source); 

if($source_headers[0] == 'HTTP/1.1 404 Not Found') { 
// First source in the array did not work execute code to try next URL in the array 
} 
else { 
// Ok we got a working source, Lets use it. 
} 
} 
?> 

回答

0

這看起來好了給我。我使用幾乎相同的代碼完成了類似的任務,並且它工作正常。至於它是正確的事情,我認爲是。你並不真正問你的「問題」。考慮修改它更具體。

相關問題