如何使用php检查页面上是否存在图像/链接


How can I check if an image/link exist on a page with php?

我正致力于为人们设计弯曲件和其他东西,然后我会给他们一个图像来放置他们的个人资料。我想在本地主机上创建一个页面,检查他们的配置文件中是否存在图像。基本上,我想看看"example1.com/myimg.jpg"是否存在于"example2.com/their profile上。

我想看看图像是否存在于外部页面上。我对php没有太多经验。有可能做这样的事吗?

这是我通常要做的:

这个想法是发出一个cURL请求来检查结果。如果是200,则资源存在。

function externalResourceExists($url) {
    $ch = curl_init($url); //$url is the url you need to check
    curl_setopt($ch, CURLOPT_NOBODY, true); //Don't get the body, we don't need it.
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);                    
    curl_close($ch);        
    return $retcode == 200;
}

然后检查函数是否返回true。