如何检查URL是否存在-错误404?(使用php) [英] how to check if a URL exists or not - error 404 ? (using php)

查看:78
本文介绍了如何检查URL是否存在-错误404?(使用php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查URL是否存在-错误404?(使用php)

how to check if a URL exists or not - error 404 ? (using php)

<?php
$url = "http://www.faressoft.org/";
?>

推荐答案

如果您拥有 allow_url_fopen ,则可以执行以下操作:

If you have allow_url_fopen, you can do:

$exists = ($fp = fopen("http://www.faressoft.org/", "r")) !== FALSE;
if ($fp) fclose($fp);

尽管严格来说,仅对于404错误,它不会返回false.可以使用流上下文来获取该信息,但是更好的选择是使用curl扩展:

although strictly speaking, this won't return false only for 404 errors. It's possible to use stream contexts to get that information, but a better option is to use the curl extension:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/notfound");
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
$is404 = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 404;
curl_close($ch);

这篇关于如何检查URL是否存在-错误404?(使用php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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