php warning redirect

2019-07-19 02:14发布

问题:

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