PDA

View Full Version : Help with errors developing Twitter API Sidebar Widget



JonH
02-11-2010, 05:36 AM
I'm trying to make a sidebar widget that can show user's Twitter activity via Twitter API. I'm able to get the plugin to work as a "non widget" but if I use the widget public function I get the following fatal error:

Notice: Undefined variable: h in /home/xxxx/public_html/xxxxxx/content/plugins/twitter_widget/twitter_widget.php on line 64

Fatal error: Call to a member function getSetting() on a non-object in /home/xxxx/public_html/xxxxxx/content/plugins/twitter_widget/twitter_widget.php on line 64

Here's the twitter_widget.php


class TwitterWidget
{
/**
* Default settings on install
*/
public function install_plugin($h)
{
// widget
$h->addWidget('twitter_widget', 'twitter_widget', ''); // plugin name, function name, optional arguments


// Default settings
if (!$h->getSetting('twitter_widget_username')) { $h->updateSetting('twitter_widget_username', ''); }
if (!$h->getSetting('twitter_widget_password')) { $h->updateSetting('twitter_widget_password', ''); }
}


public function header_include($h)
{
$h->includeCss('twitter_widget');
}

public function widget_twitter_widget()

{

// your twitter username and password
$twitter_widget_username = $h->getSetting('twitter_widget_username');
$twitter_widget_password = $h->getSetting('twitter_widget_password');
// for Twitterlibphp
$twitter_widget_username = $username;
$twitter_widget_password = $password;

// include Twitterlibphp
require_once(PLUGINS . 'twitter_widget/libs/twitter_lib.php');

// initialize the twitter class
$twitter = new Twitter($username, $password);

// fetch your profile in xml format
$xml = $twitter->getFriendsTimeline();

// fetch your session xml format
$twitter_status = new SimpleXMLElement($xml);

// show twitter widget template

echo '<div class="twitter_container">';

foreach($twitter_status->status as $status){
echo '<div class="twitter_status">';
foreach($status->user as $user){
echo '<img src="'.$user->profile_image_url.'" class="twitter_image">';
echo '<a href="http://www.twitter.com/'.$user->screen_name.'">'.$user->name.'</a>: ';
}
echo $status->text;
echo '<br/>';
echo '<div class="twitter_posted_at"><strong>Posted at:</strong> '.$status->created_at.'</div>';
echo '</div>';
echo '</div>';
}

}

}


Going by the Hello_world widget doc you don't have to make a plugin hook but something like public function "widget_twitter_widget()" (no $h) but I'm guessing that seems to keep the $h->getSetting to get the username and password. :confused: Thanks.

Nick
02-11-2010, 06:54 AM
The $h parameter is actually sent to the function, so you should just be able to do this and have it work as expected:


public function widget_twitter_widget($h) In theory anyway!

Edit:

One problem is that widgets aren't called from the pluginHook function, but rather directly from the Widgets plugin. That means it's unlikely that $h->plugin->folder is set as "twitter_widget" so when you use the getSetting function, do it like this instead:


$twitter_widget_username = $h->getSetting('twitter_widget_username', 'twitter_widget');
$twitter_widget_password = $h->getSetting('twitter_widget_password', 'twitter_widget');

JonH
02-11-2010, 07:58 AM
public function widget_twitter_widget($h)

I tried that already, it didn't work



$twitter_widget_username = $h->getSetting('twitter_widget_username', 'twitter_widget');
$twitter_widget_password = $h->getSetting('twitter_widget_password', 'twitter_widget');

I tried your idea and it didn't work turning it on and off the uninstalled cleared cache, installed afterward of course.
Unless you have another simple trick up your sleeve I can post the zip file or send it over to you if you want to look at it Nick.

Thanks!

Nick
02-11-2010, 01:43 PM
Got the .zip you sent me and I have it working now. It's on the 1.0 Branch and just needs some work on the presentation.

JonH
02-12-2010, 12:09 PM
Thanks! Almost done.

JonH
02-13-2010, 02:46 AM
Well I thought it was until I realized that Twitter limits the API calls to 150/hr so it needs a cache method to update say once every 15 minutes or so. Googling for a snippet...unless Hotaru's built in caching would work somehow?

Nick
02-13-2010, 03:23 AM
You could use Hotaru's smartCache, but it uses database updates to tell it when to update.

http://hotarucms.org/showthread.php?269-Hotaru-s-Different-Caching-Methods

Use the section under "For HTML code:".

You'll need to change some of it. For example, in this line:


$output = $h->smartCache('html', 'posts', 10, '', 'top_posts');

It updates the cache whenever there's an update to the "posts" table or otherwise every 10 minutes. The cache file is tagged with "top_posts". In your case, you might do something like this:


$output = $h->smartCache('html', 'activity', 60, '', 'twitter_widget');

So that would update whenever there's a change to the activity table (which means you'd need the widget to "require" the Activity plugin. Your Twitter API call would go in here:


$output = // HTML CODE HERE

assigning the result to $output.

You don't have to choose the activity table to determine the update, maybe the users one would be better. As long as there are less than 150 updates an hour, you'll be okay.