views:

267

answers:

2

I am trying to get twitter updates like this:

try {

  $doc = new DOMDocument();
  $doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');
  $isOK = true;

} catch( Zend_Exception $e ) {
  $isOK = false;
}

If there is not problem with internet connection then $isOK = true; is set. But if there is a problem in loading twitter page then it shows following warnings and does not set $isOK = false;

Warning: DOMDocument::load(http://twitter.com/statuses/user_timeline/1234567890.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /var/www/vcred/application/controllers/IndexController.php on line 120

I don't want to see above warning on my webpage in any case. Any idea?

Thanks

+1  A: 
ini_set('display_errors',0);

it should be set this way on any production site.
users shouldn't be allowed to see system error messages at all

Col. Shrapnel
+4  A: 

Several options:

Suppress all errors for just this function call

@$doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');

which is the same as

$oldLevel = error_reporting(0);
$doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');
error_reporting($oldLevel);

Suppressing errors this way is generally frowned upon, as it makes code harder to debug. Like Shrapnel pointed out, you want to disable public display of all error messages on a production system anyway. On Dev systems you are encouraged to use error_reporting(-1);, which would enable E_ALL and E_STRICT.

If you want to use try/catch, you can also change default error handling and convert all errors to exceptions by doing

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    throw new Exception($errstr, $errno);
}
set_error_handler("myErrorHandler");

This is a global change though and affects everything raised. You'd also have to use catch(Exception $e) instead of Zend_Exception then in your code, but it would work. Note that the above would convert everything, even Notices, so you will also get an Exception about $isOk being undefined if you are trying to access this later. Feel free to adapt the handler to your liking and check out the user comments for set_error_handler for more refined versions.

Another global change would be to change the application.ini in your application folder, e.g. letting Zend Framework control error handling:

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

Change these to your needs. They are the same as in PHP.ini, e.g.

display_errors:

This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user.

display_startup_errors:

Even when display_errors is on, errors that occur during PHP's startup sequence are not displayed. It's strongly recommended to keep display_startup_errors off, except for debugging.

Gordon
+1 for detail and information.
NAVEED
@Gordon How can add your Answer to my favorities!? ;) Howewer… you forgot to write about `ini_set`, .htaccess and recompiling PHP with source code changes of default error reporting level ;)
takeshin
@takeshin thanks. I don't think we can fav answers. Shrapnel already mentioned `ini_set` so I didn't. No use in repeating what others said unless it contains something new imho. Also, my answers generally do not intend to cover each and every possible solution. Leaves some room for other answers :)
Gordon
@Gordon Have considered changing your nickname to 'PHP Community Wiki'? ;) Anyway, thanks for all the answers, they are always a good reading.
takeshin
@takeshin CWs dont give rep ;) you're welcome.
Gordon