【发布时间】:2009-05-21 12:59:17
【问题描述】:
我有一个文件可以打开一个 URL 并读取它并进行解析。 现在,如果该 URL 变为 dwon 并且我的文件无法打开它,那么我需要的是应该生成错误邮件,但在终端或 konsole 上不应出现错误消息。 我怎样才能做到这一点? 请帮忙!!
【问题讨论】:
标签: php error-handling
我有一个文件可以打开一个 URL 并读取它并进行解析。 现在,如果该 URL 变为 dwon 并且我的文件无法打开它,那么我需要的是应该生成错误邮件,但在终端或 konsole 上不应出现错误消息。 我怎样才能做到这一点? 请帮忙!!
【问题讨论】:
标签: php error-handling
你总是可以做这样的事情(我假设你正在使用 file_get_contents)
$file = @fopen("abc.com","rb");
if(!$file) {
@mail(.......);
die();
}
//rest of code. Else is not needed since script will die if hit if condition
【讨论】:
如果您通过网络检索文件,请改用 curl。它内置了错误处理,它会告诉你发生的错误。使用 file_get_contents 不会告诉你出了什么问题,它也不会遵循重定向。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/file');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
if ( $result == false ) {
$errorInfo = curl_errno($ch).' '.curl_error($ch);
mail(...)
} else {
//Process file, $result contains file contents
}
【讨论】:
if (!$content = file_get_contents('http://example.org')) {
mail(...);
}
【讨论】: