如何显示自定义错误而不是显示简单的html dom.php错误


How to display custom error instead of displaying simple html dom.php error

我正在做一个项目,该项目只是简单地废弃了youtube的评论,但条件是不使用cURL。所以我开始使用简单的html dom.php。我几乎与许多问题作斗争,现在这些问题已解决。但是我有一个查询,如何为 file_get_html() 方法设置错误。例如:我允许用户输入 youtube 视频的网址,我几乎对 youtube 网址进行了大量验证,但如果有人输入错误的网址,那么脚本将报告此错误:*警告:file_get_contents(http://www.youtube.com/watch?v=VR--anQO?????222cV--clS8):无法打开流:HTTP 请求失败!HTTP/1.1 404 在 C:''xampp''htdocs''utube''simple_html_dom.php 第 39 行中找不到*

而且我无法将错误报告设置为 false,因为它是不允许的。所以你能告诉我如何显示这个错误:无效的URL而不是上面显示的错误。是的,请不要说YouTube的API,因为它是不允许的:(

最简单的方法是使用 @。只需像这样使用它:

if (@file_get_html(...)) {
    //do whatever you want, if it suceeds
} else {
    echo "Invalid URL";
}

您可以在此处找到更多信息。

由于simple_html_dom是一个类,这应该是@没有影响的原因,因为类似乎并不认为它。

值得一试的事情:首先尝试使用file_get_contents,如果可行,则只使用simle_html_dom - 如下所示:

if (@file_get_contents(...)) {
    file_get_html(...);
    //now anything else you want to do
} else {
    echo "Invalid URL";
}