0

所有,我有以下代码来检索一些推文:

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;

然后我有以下代码将它们放入滑块中:

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
}
?>

这段代码大约有 90% 的时间工作,并根据用户名等抓取推文。然而,大约 10% 的时间它只是旋转而不显示任何文本。有没有人看到我可以改进这个解决方案以便始终显示推文的任何东西或方法?从错误处理的角度来看,如果它无法检索推文,我是否可以添加任何内容以不显示它?

感谢您提前提供任何建议。

4

1 回答 1

0

您可以尝试检查 twitter 返回的内容是否实际上是推文列表。目前,您只检查 twitter 是否返回任何内容。

// Check to make sure we have a tweet and display it.
if ( is_array($tweet) && isset($tweet[0]->id_str) ) {
    do_action( 'themeblvd_twitter_slider', $tweet, $options, $username, $slider_id );
} else {
    $output = 'Twitter timed out.';
}
于 2012-05-01T21:55:23.917 回答