【问题标题】:Linkifying tweets using simplepie使用 simplepie 链接推文
【发布时间】:2012-08-22 21:40:00
【问题描述】:

我正在尝试使某些推文中包含的链接在我的推特小部件上工作,例如,当我推特一张图片并且推特将其变成一个短链接时。这是代码...提前感谢您的帮助!

if ( !function_exists( 'wp_echo_twitter' ) ) {
    function wp_echo_twitter($username) {
        include_once( ABSPATH . WPINC . '/class-simplepie.php' );

        // Fetch feed, set cache locaiton, and initialize function
        $feed = new SimplePie();
        $feed->set_feed_url("http://twitter.com/statuses/user_timeline/$username.atom?count=200");
        $feed->set_cache_location( ABSPATH . WPINC );
        $feed->init();
        $feed->handle_content_type();

        // Output tweet
        foreach ($feed->get_items(0, 1) as $item):
            echo '<p class="hero-p" style="margin-bottom: 9px;">"' . $item->get_description() . '"</p>' . '<span><a href="' . $item->get_permalink() . '">' . $item->get_date('D, M j, Y') . '</a></span>';
        endforeach;

    }
}

【问题讨论】:

    标签: php twitter hyperlink simplepie linkify


    【解决方案1】:
    require_once('simplepie.inc');
    $cache = "/cache";
    $duration = 3600;
    
    // turn urls, hashtags and @replies into links
    function twitterify($ret) {
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a target=\"_new\" href=\"\\2\" >\\2</a>", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a target=\"_new\" href=\"http://\\2\" >\\2</a>", $ret);
    $ret = preg_replace("/@(\w+)/", "<a target=\"_new\" href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
    $ret = preg_replace("/#(\w+)/", "<a target=\"_new\" href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret);
    return $ret;
    }
    
    //  make the time twittery
    function niceTime($time) {
    $delta = time() - $time;
    if ($delta < 60) {
    return 'less than a minute ago.';
    } else if ($delta < 120) {
    return 'about a minute ago.';
    } else if ($delta < (45 * 60)) {
    return floor($delta / 60) . ' minutes ago.';
    } else if ($delta < (90 * 60)) {
    return 'about an hour ago.';
    } else if ($delta < (24 * 60 * 60)) {
    return 'about ' . floor($delta / 3600) . ' hours ago.';
    } else if ($delta < (48 * 60 * 60)) {
    return '1 day ago.';
    } else {
    return floor($delta / 86400) . ' days ago.';
    }
    }
    
    function getLastTweet($username){
    $twitter = new SimplePie("http://twitter.com/statuses/user_timeline/$username.rss", $cache, $duration);
    foreach ($twitter->get_items(0,1) as $item) :
    $pubDate = $item->get_item_tags('','pubDate');
    $data = $pubDate[0]['data'];
    $time = niceTime(strtotime(str_replace("+0000", "", $data)));
    
    echo "<p>"; 
    echo twitterify(str_replace("$username: ", "", $item->get_title()));
    echo "<span>$time</span>"; 
    echo "</p>";
    endforeach;
    }
    // show tweet
    getLastTweet("twitterusername");
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多