Home arrow PHP arrow Page 3 - Error Handling for Dynamic Twitter Signature Images with PHP

Throwing exceptions for errors - PHP

Over the course of my past two articles Iíve been showing you how to build a script capable of creating and displaying a dynamic signature image containing the latest status from a userís Twitter feed. In the third installment in this series, I will be demonstrating how to add proper object-oriented error handling to the SignatureImage class.

TABLE OF CONTENTS:
  1. Error Handling for Dynamic Twitter Signature Images with PHP
  2. Understanding error-handling
  3. Throwing exceptions for errors
  4. Playing throw and catch
By: Nilpo
Rating: starstarstarstarstar / 1
August 25, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Letís move back through our code and replace all of the error comments with throw statements.

    private function fetchUserInfo($name)

    {

        $url = "http://twitter.com/statuses/user_timeline/{$name}.xml?count=1";

        $xml = $this->curlRequest($url);

        if ($xml === false) {

            // User feed unavailable.

        }

        $statuses = new SimpleXMLElement($xml);

        if (!$statuses || !$statuses->status) {

            // Invalid user channel.

        }

       foreach ($statuses->status as $status) {

            $this->status_text   = (string) $status->text;

            $this->profile_image = (string) $status->user->profile_image_url;

            $this->screen_name   = (string) $status->user->screen_name;

            break;

        }

    }

We first encounter error states in the fetchUserInfo method.  It has two different error states. One occurs when the user feed is unavailable, and the other occurs when the script encounters an invalid user channel.

    private function fetchUserInfo($name)

    {

        $url = "http://twitter.com/statuses/user_timeline/{$name}.xml?count=1";

        $xml = $this->curlRequest($url);

        if ($xml === false) {

            throw new Exception('User feed unavailable.');

        }

        $statuses = new SimpleXMLElement($xml);

        if (!$statuses || !$statuses->status) {

            throw new Exception('Invalid user channel.');

        }

       foreach ($statuses->status as $status) {

            $this->status_text   = (string) $status->text;

            $this->profile_image = (string) $status->user->profile_image_url;

            $this->screen_name   = (string) $status->user->screen_name;

            break;

        }

    }

The conversion process is very easy.  Simply replace all of the comments with throw statements.  The comment itself works very nicely as the error message string for the Exception class.  For the sake of space I wonít show you every instance, but you can follow this simple example to complete each of the remaining replacements.



 
 
>>> More PHP Articles          >>> More By Nilpo
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: