(自我回答/总结)
Dragoste 和 Enapupe 的回答都有效。根据 Enapupe 的回答,我将它包装成一个包罗万象的函数,并添加了一个可选的文件类型参数。希望它可以帮助某人...
function isFileUrl($url, $type=''){
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_VERBOSE => false
));
curl_exec($ch);
$gh = curl_getinfo($ch);
if($type!==''){
// add types here (see http://reference.sitepoint.com/html/mime-types-full for list)
if($type=='img'){
$typeArr = array('image/jpeg', 'image/png');
}
elseif($type=='pdf'){
$typeArr = array('application/pdf');
}
elseif($type=='html'){
$typeArr = array('text/html');
}
if($gh['http_code'] === 200 && in_array($gh['content_type'], $typeArr)){
$trueFalse = true;
}
else {
$trueFalse = false;
}
}
else {
if($gh['http_code'] === 200){
$trueFalse = true;
}
else {
$trueFalse = false;
}
}
return $trueFalse;
}
//test - param 1 is URL, param 2 (optional) can be img|pdf|html
if(isFileUrl('https://www.google.com/images/srpr/logo4w.png', 'img')==true){
// do stuff
echo 'TRUE';
}
else {
echo 'FALSE';
}