All,
I have the following code to retrieve some Tweets:
if( ! $tweet ) {
$url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name={$username}&count={$how_many}";
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$contents = curl_exec( $curl );
curl_close( $curl );
if ( ! empty( $contents ) ) {
// Decode it.
$tweet = json_decode( $contents );
}
}
// Check to make sure we have a tweet and display it.
if ( $tweet ) {
do_action( 'themeblvd_twitter_slider', $tweet, $options, $username, $slider_id );
} else {
$output = 'Twitter timed out.';
}
return $output;
I then have the following code to put them in a Slider:
function themeblvd_twitter_slider_default( $tweet, $options, $username, $slider_id ) {
$tweet = (array)$tweet;
$classes = themeblvd_get_classes( 'slider_standard', true );
$classes .= ' hide-nav_standard';
$classes .= ' show-nav_arrows';
$classes .= ' show-pause_play';
//themeblvd_twitter_slider_js( $slider_id, $options );
?>
<div id="tb-slider-<?php echo $slider_id; ?>" class="slider-wrapper standard-slider-wrapper">
<div class="slider-inner<?php echo $classes; ?>">
<div class="slides-wrapper slides-wrapper-<?php echo $slider_id; ?>">
<div class="slides-inner">
<div class="slider standard-slider flexslider">
<div class="tb-loader"></div>
<ul class="slides">
<?php
foreach($tweet as $t){
?>
<li class="slide tight <?php echo $classes; ?>">
<div class="slide-body">
<div class="grid-protection">
<?php
echo '<span class="tweet-icon '.$options['icon'].'"></span>';
echo '<a href="http://twitter.com/'.$username.'/status/'.$t->id_str.'" target="_blank">';
echo $t->text;
echo '</a>';
?>
</div><!-- .grid-protection (end) -->
</div><!-- .slide-body (end) -->
</li>
<?php
}
?>
</ul>
</div><!-- .slider (end) -->
</div><!-- .slides-inner (end) -->
</div><!-- .slides-wrapper (end) -->
</div><!-- .slider-inner (end) -->
<div class="design-1"></div>
<div class="design-2"></div>
<div class="design-3"></div>
<div class="design-4"></div>
</div><!-- .slider-wrapper (end) -->
<?php
}
?>
This code works about 90% of the time and grabs the tweets based on a username etc. However, about 10% of the time it just rotates without displaying any of the texts. Does anyone see anything or a way that I can make this solution better so that the tweets are always displayed? Is there anything from an error handling perspective that I’m missing that I could add to not display it if it can’t retrieve the tweets?
Thanks for any advice in advance.
You could try to check if what twitter returns actually is a list of tweets. At the moment you only check if twitter returns anything at all.