php warning redirect

僤鯓⒐⒋嵵緔 提交于 2019-12-10 23:57:08

问题


I use snmp2_real_walk function.

$tmp = snmp2_real_walk($ip, '***'.$vlan, $title, 100000,10);

When oid is correct and the device is working i get the desired output. But i want to handle warnings: Invalid object identifier... or No response from 192.168.19.249... or whatever. My problem is: how can i either redirect those warnings into my variable?

or is there some another function which shows these warnings?

Thanks!


回答1:


You could also have a look at set_error_handler to set your own error handler for this specific case, and then restore the error handler after making the function call.




回答2:


using error_get_last() was the solution :)




回答3:


Take a look at PHP's set_error_handler function. snmp2_real_walk generates E_WARNING messages when it encounters an error, set_error_handler will allow you to capture these and log them etc.




回答4:


You want to use a try-catch block to catch your exception here's an example from PHP's documentation adjusted to your function:

<?php

try 
{
    $tmp = snmp2_real_walk($ip, '***'.$vlan, $title, 100000,10);

} 
catch (Exception $e) 
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

echo $tmp;
?>

The PHP Manual Page: http://php.net/manual/en/language.exceptions.php



来源:https://stackoverflow.com/questions/6731877/php-warning-redirect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!