ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to make the Twitter plugin return false

April 24, 2008 10:26pm

Subscribe [4]
  • #1 / Apr 24, 2008 10:26pm

    nek4life

    32 posts

    I like to know if there’s a way to make the Twitter Timeline plugin return false if the service is down.  I’d like to use the Twitter to create a mini-news section on one of my sites.  I just don’t want people clicking on a page and have nothing show up at all like the plugin does now when the service is down.  Any Ideas?

  • #2 / Apr 25, 2008 3:01am

    George Ornbo

    272 posts

    I’m not sure it is in the 1st Party Plugin but you do it by evaluating the HTTP status code

    There’s an example here that you could probably port in

  • #3 / Apr 25, 2008 10:01am

    Derek Jones

    7561 posts

    nek4life, you can modify the plugin around line 97:

    if ($rawxml == '' OR substr($rawxml, 0, 5) == "<html")
    {
        $TMPL->log_item("Twitter Timeline error: Unable to retreive statuses from Twitter.com");
        $this->return_data = '';
        return;
    }

    change to:

    if ($rawxml == '' OR substr($rawxml, 0, 5) == "<html")
    {
        $TMPL->log_item("Twitter Timeline error: Unable to retreive statuses from Twitter.com");
        return $this->return_data = $TMPL->no_results();
    }

    Then in your plugin you can use

    {if no_results}
        Sorry, Twitter has some lolcats chewing on the power couplings and the service is down.
    {/if}
  • #4 / Apr 25, 2008 11:09am

    nek4life

    32 posts

    Aha, so by changing that line of code I’m adding a no_result variable to work with.  This is great, I really need to poke around the back end code more.  I can’t wait until everything is code igniter once that happens I’m going to start digging into the framework so I can understand how this stuff work better.  Thanks Derek I’ll give this a shot later tonight.  I’ll let you know if I get it working.

    Also thank you George for your contribution.

  • #5 / Apr 25, 2008 11:18am

    Derek Jones

    7561 posts

    No problem, and incidentally, the solution linked in George’s post is a good one.  The plugin takes the simple way out, that if anything other than valid XML was retrieved, bail out.  You could use a combination of the two to give more detailed responses, if you wanted.

  • #6 / Apr 25, 2008 11:22am

    nek4life

    32 posts

    nek4life, you can modify the plugin around

    {if no_results}
        Sorry, Twitter has some lolcats chewing on the power couplings and the service is down.
    {/if}

    Haha, I just noticed this… I’m sure they have that problem all the time.

  • #7 / Apr 25, 2008 11:37am

    nek4life

    32 posts

    I haven’t rolled my own PHP for a while so I may just take the easy way out myself at this point, but I may try to tinker with George’s suggestion in the future.  It looks like that solution may come in handy for any number of services.

  • #8 / Jun 22, 2008 1:33am

    Wayde Christie

    24 posts

    Hey Derek,

    I just followed these steps myself and I’m getting unexpected results.

    Here’s my code:

    {exp:twitter_timeline type="user" user="xxx" password="xxx" limit="1" cache="yes" refresh="15" twitter_refresh="15"}
    {if no_results}
    <blockquote>Sorry, Twitter has some lolcats chewing on the power couplings and the service is down.</blockquote>
    {if:else}
    <blockquote>
        {text}
        <cite>{created_at format="%l %j%S %F, %g:%i%a"}</cite>
    </blockquote>
    {/if}
    {/exp:twitter_timeline}

    My template isn’t outputting anything at the moment.

    Am I doing anything wrong?

  • #9 / Jun 22, 2008 2:37am

    Derek Jones

    7561 posts

    Well, the no_results conditionals do not need (nor use) an if:else.  If there are no results, only the no_results conditional content will be displayed; if there are results, the no_results content is ignored and the rest of the content is displayed.  But I would also check the Template Parsing log (Output and Debugging prefs) to see if there are any debugging messages from the plugin.

  • #10 / Jun 22, 2008 2:46am

    Wayde Christie

    24 posts

    Thanks Derek - that did the trick.

    No issues with the template parsing log either btw.

    Cheers 😊

  • #11 / Jun 22, 2008 3:46am

    Matthew Pennell

    221 posts

    Personally I’ve given up using the Twitter plugin due to the potential for lengthy page load delays when Twitter has problems. Instead I am polling the API with a hand-rolled cURL script and writing the output to a static HTML page that I can then include() in my templates.

    require_once('curl.class.php');
    $curl = new Curl();
    
    $twitter = $curl->fetch_xml('http://twitter.com/statuses/user_timeline/matthewpennell.xml?count=1');
    
    if ($twitter)
    {
        $output = '<div id="twitter">/i/twitterific.png<h2>Twittering</h2><blockquote><p>';<br />
        $in = array(<br />
            '/^(is\s)?[a-z]/',<br />
            '/(https?:\/\/[A-Za-z0-9\.\-\_\/\?\=]+)/',<br />
            '/@(\w+)/'<br />
        );<br />
        $out = array(<br />
            '<span>$2</span>',<br />
            '<a href="http://$1">[link]</a>',<br />
            '<a href="http://www.twitter.com/$1">@$1</a>'<br />
        );<br />
        foreach ($twitter->status as $status)<br />
        {<br />
            $output .= preg_replace($in, $out, $status->text);<br />
        }<br />
        $output .= '</p>
    </blockquote><p></div>';<br />
        # Write the final information to a static HTML file<br />
        $fh = fopen('twitter.html', 'w');<br />
        fwrite($fh, $output);<br />
        fclose($fh);<br />
        echo "Twitter import completed successfully.\n\n";<br />
    }    <br />
    else<br />
    {<br />
        echo "Twitter import failed - could not connect to API\n\n";<br />
    }<br />
    $curl->close();

    It means that even when Twitter is down, you still have some content to display.

  • #12 / Jun 22, 2008 11:36am

    Derek Jones

    7561 posts

    Interesting idea, Buddy, though why not just put the Twitter plugin, or your own solution if you prefer it, into a separate template, and fetch its contents via AJAX?  The plugin and EE have their own caching mechanisms which you can take advantage of, and that would eliminate the issue of bogging the loading of the requested page during times that its not retrieving from the cache.

  • #13 / Jun 22, 2008 4:10pm

    Matthew Pennell

    221 posts

    I try not to rely on Ajax to post-load page content, I’d rather have it rendered as part of the actual HTML. The cURL script is run as a cron job, so it has no effect on the speed of the requested page; and the cURL request sets an If-Modified-Since header to reduce the load on the third-party API.

    So there. 😉

    Congratulations on the new job, btw. 😊

  • #14 / Jun 22, 2008 6:15pm

    Derek Jones

    7561 posts

    Yes, mighty convenient if you have the option to run actual cron jobs on your site.  And thanks!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases