Twitter module while stuck with PHP 5.1.6

Building a new site today assumes that everything social should come with the box -- not even in it. Within the first question a client asks is how this whole twitter-thing works, and how he can use it. Of course Drupal provides modules to integrate with these, but what if you're stuck on a host  "playing it safe" and sticking with PHP 5.1.6 ( that is, all RedHat, CentOS providers that won't use custom compiled packages )?

Well, Facebook isn't much of a problem, since the easiest way is to either create an app for that, if you feel like inventing the wheel at every possible turn (there doesn't seem to be a module to do that for your just yet), or simply provide an RSS feed to an existing consumer app ( I personally prefer Graffity RSS  .

You can take a similar approarch with Twitter, by using twitterfeed , however in this case you also have the option of a module, which provides more options -- and we just love more control, don't we?

Setting the modules up is quite straight forward if you're using PHP 5.2 or higher. However, this blog is hosted on a CentOS server, with PHP 5.1.6 . This creates two major problems with all things social:

  • they rely on JSON, which your provider will most likely not want to add ( the argument in my case was that since they've gone this long without it, most people will have installed custom solutions which would probably break if they did add it)
  • the JSON wrappers available are not 100% like the original PHP function in terms of returned values

So, in my case, what needed to be done is add JSON functionality to my site in general ( always good to have it ), and patch up the Twitter module to work with it. 

  • To enable JSON, download the JSON wrapper script from here . To enable it for all of my Drupal installation, I did it the hacky way -- unzip and upload to the root of your site, add the line 
    require './jsonwrapper/jsonwrapper.php';
    to the top of your index.php (right before the require_once statement), and most of your modules won't know the difference.
  • Twitter module however had some trouble with this - it was trying to access the returned data as an array, when it was returned as an object. Nothing to hard to fix, really -- just semantics. So, in order to keep it compatible, just patch the code at twitter.lib.php in two places.
    • Around line 387, replace the entire constructor function with this block
      public function __construct($values = array()) {
          if ( is_array($values) ) {
      		$this->created_at = $values['created_at'];
      		$this->id = $values['id'];
      		$this->text = $values['text'];
      		$this->source = $values['source'];
      		$this->truncated = $values['truncated'];
      		$this->favorited = $values['favorited'];
      		$this->in_reply_to_status_id = $values['in_reply_to_status_id'];
      		$this->in_reply_to_user_id = $values['in_reply_to_user_id'];
      		$this->in_reply_to_screen_name = $values['in_reply_to_screen_name'];
      		if (isset($values['user'])) {
      		  $this->user = new TwitterUser($values['user']);
      		}
      	} else {
      		$this->created_at = $values->created_at;
      		$this->id = $values->id;
      		$this->text = $values->text;
      		$this->source = $values->source;
      		$this->truncated = $values->truncated;
      		$this->favorited = $values->favorited;
      		$this->in_reply_to_status_id = $values->in_reply_to_status_id;
      		$this->in_reply_to_user_id = $values->in_reply_to_user_id;
      		$this->in_reply_to_screen_name = $values->in_reply_to_screen_name;
      		if (isset($values->user)) {
      		  $this->user = new TwitterUser($values->user);
      		}
      	}
        }
      
      }
    • Around line 475, replace the constructor function with this block
      public function __construct($values = array()) {
          if ( is_array($values) ) {
      		$this->id = $values['id'];
      		$this->screen_name = $values['screen_name'];
      		$this->name = $values['name'];
      		$this->location = $values['location'];
      		$this->description = $values['description'];
      		$this->url = $values['url'];
      		$this->followers_count = $values['followers_count'];
      		$this->friends_count = $values['friends_count'];
      		$this->statuses_count = $values['statuses_count'];
      		$this->favourites_count = $values['favourites_count'];
      		$this->protected = $values['protected'];
      		$this->profile_image_url = $values['profile_image_url'];
      		$this->profile_background_color = $values['profile_background_color'];
      		$this->profile_text_color = $values['profile_text_color'];
      		$this->profile_link_color = $values['profile_link_color'];
      		$this->profile_sidebar_fill_color = $values['profile_sidebar_fill_color'];
      		$this->profile_sidebar_border_color = $values['profile_sidebar_border_color'];
      		$this->profile_background_image_url = $values['profile_background_image_url'];
      		$this->profile_background_tile = $values['profile_background_tile'];
      		$this->verified = $values['verified'];
      		$this->created_at = $values['created_at'];
      		if ($values['created_at'] && $created_time = strtotime($values['created_at'])) {
      		  $this->created_time = $created_time;
      		}
      		$this->utc_offset = $values['utc_offset'];
      
      		if ($values['status']) {
      		  $this->status = new TwitterStatus($values['status']);
      		}
      	} else {
      		$this->id = $values->id;
      		$this->screen_name = $values->screen_name;
      		$this->name = $values->name;
      		$this->location = $values->location;
      		$this->description = $values->description;
      		$this->url = $values->url;
      		$this->followers_count = $values->followers_count;
      		$this->friends_count = $values->friends_count;
      		$this->statuses_count = $values->statuses_count;
      		$this->favourites_count = $values->favourites_count;
      		$this->protected = $values->protected;
      		$this->profile_image_url = $values->profile_image_url;
      		$this->profile_background_color = $values->profile_background_color;
      		$this->profile_text_color = $values->profile_text_color;
      		$this->profile_link_color = $values->profile_link_color;
      		$this->profile_sidebar_fill_color = $values->profile_sidebar_fill_color;
      		$this->profile_sidebar_border_color = $values->profile_sidebar_border_color;
      		$this->profile_background_image_url = $values->profile_background_image_url;
      		$this->profile_background_tile = $values->profile_background_tile;
      		$this->verified = $values->verified;
      		$this->created_at = $values->created_at;
      		if ($values->created_at && $created_time = strtotime($values->created_at)) {
      		  $this->created_time = $created_time;
      		}
      		$this->utc_offset = $values->utc_offset;
      
      		if ($values->status) {
      		  $this->status = new TwitterStatus($values->status);
      		}
      	}
        }
      
    • All done!
  • Alternatively, you can just download the patched library file from here.