2009-05-21 105 views
1

我有一个文件打开一个URL并读取它并进行解析。 现在,如果该URL去dwon,我的文件无法打开它,那么我需要的是一个错误邮件应该生成,但终端或konsole应该出现错误信息。 我该怎么做? Plz help !!php中的错误处理

回答

2

你总是可以做这样的事情(我假设你正在使用的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 
+0

这就是使用何种即时通讯做,即时通讯@fopen代替@file_get_contents,但我想要的是使用这种代码的IM,当我执行终端上得到错误信息在终端/ Konsole.By应该NOE出现的错误它在那里。我希望没有错误消息出现在termianl上。 – developer 2009-05-21 13:11:22

0
if (!$content = file_get_contents('http://example.org')) { 
    mail(...); 
} 
1

使用卷曲,而不是如果你在网络上检索文件。它内置了错误处理,它会告诉你发生的错误。使用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 
}