检查数组中的多个URL是否存在,但仅当数组中的第一个URL不存在时才检查


Check multiple URL in an array if they exist but only if first URL in array does not exist

我试图创建一个PHP脚本,检查是否存在一个URL。我在数组中得到了多个URL,我想检查,但只有当数组$sources中的第一个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.
}
}
?>

我看还行。我用几乎相同的代码做了一个类似的任务,它工作了。就这样做是对的而言,我认为是对的。你并没有把"问题"问得很清楚。