SimpleXML and PHP Error Handling – Tumblr RSS Example

Overview

Use PHP’s if…else statement to hide error messages on a web page when an imported RSS feed is unavailable.

Sample error message(s):

Warning: simplexml_load_file(http://username.tumblr.com/no-rss) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/user/public_html/feed.php on line 123

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity “http://username.tumblr.com/no-rss” in /home/user/public_html/feed.php on line 123

Requirements

  • Web server with PHP installed
  • FTP or shell access
  • Text editor

Installation

Note: The following example uses a PHP foreach loop to limit the number of feed items displayed.

  1. Change the “7” in the second if statement to the desired number of items.  The default number of items in a Tumblr RSS feed is 20.
     
  2. Replace the URI in the simplexml_load_file() function with the appropriate feed.
     
  3. Replace the output in the second echo statement with a corresponding message.
  4. Save the web page as a PHP file.  Test the new feed in a web browser.
     
  5. [Optional] Enter a broken link (in step 2) and the message (in step 3) will display.

Reference:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: