如何通过 PHP 检查 URL 是否存在? [英] How can I check if a URL exists via PHP?

查看:43
本文介绍了如何通过 PHP 检查 URL 是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查 PHP 中是否存在 URL(不是 404)?

How do I check if a URL exists (not 404) in PHP?

推荐答案

这里:

$file = 'http://www.example.com/somefile.jpg';
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}

来自这里正下方上面的帖子,有一个curl 解决方法:

From here and right below the above post, there's a curl solution:

function url_exists($url) {
    return curl_init($url) !== false;
}

这篇关于如何通过 PHP 检查 URL 是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆