2009-05-21 109 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 
}