douyouzheng2209 2013-02-11 14:44
浏览 24

保存推文而不是回显/列出项目

I am using an open source twitter.class.php to query/search for particular terms on Twitter.

I don't want to echo everything out. I want to save the output to a database.

How do I replace the echo and save each record to a database?

I don't know how to break getTweets($termt, 100);

The 100 makes it get 100 tweets at once.

require('twitter.class.php');
$twitter = new twitter_class();
echo $twitter->getTweets($termt, 100);

Note: i don't know how to use an array. Help me, please.

Thank you.

This is my Twitter.class.php

function twitter_class()
{
    $this->realNamePattern = '/\((.*?)\)/';
    $this->searchURL = 'http://search.twitter.com/search.atom?rpp=500&lang=en&q=';

    $this->intervalNames   = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
    $this->intervalSeconds = array( 1,        60,       3600,   86400, 604800, 2630880, 31570560);

    $this->badWords = array('somebadword', 'anotherbadword');
}

function getTweets($q, $limit=500)
{
    $output = '';

    // get the seach result
    $ch= curl_init($this->searchURL . urlencode($q));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    $response = curl_exec($ch);

    if ($response !== FALSE)
    {
        $xml = simplexml_load_string($response);

        $output = '';
        $tweets = 0;

        for($i=0; $i<count($xml->entry); $i++)
        {
            $crtEntry = $xml->entry[$i];
            $account  = $crtEntry->author->uri;
            $image    = $crtEntry->link[1]->attributes()->href;
            $tweet    = $crtEntry->content;

            // skip tweets containing banned words
            $foundBadWord = false;
            foreach ($this->badWords as $badWord)
            {
                if(stristr($tweet, $badWord) !== FALSE)
                {
                    $foundBadWord = true;
                    break;
                }
            }

            $tweet = str_replace('<a href=', '<a target="_blank" href=', $tweet);

            // skip this tweet containing a banned word
            if ($foundBadWord)
                continue;

            // don't process any more tweets if at the limit
            if ($tweets==$limit)
                break;
            $tweets++;

            // name is in this format "acountname (Real Name)"
            preg_match($this->realNamePattern, $crtEntry->author->name, $matches);
            $name = $matches[1];

            // get the time passed between now and the time of tweet, don't allow for negative
            // (future) values that may have occured if server time is wrong
            $time = 'just now';
            $secondsPassed = time() - strtotime($crtEntry->published);

            if ($secondsPassed>0)
            {
                // see what interval are we in
                for($j = count($this->intervalSeconds)-1; ($j >= 0); $j--)
                {
                    $crtIntervalName = $this->intervalNames[$j];
                    $crtInterval = $this->intervalSeconds[$j];

                    if ($secondsPassed >= $crtInterval)
                    {
                        $value = floor($secondsPassed / $crtInterval);
                        if ($value > 1)
                            $crtIntervalName .= 's';

                        $time = $value . ' ' . $crtIntervalName . ' ago';

                        break;
                    }
                }
            }

            $output .= '
            <div class="tweet">
                <div class="avatar">
                    <a href="' . $account . '" target="_blank"><img src="' . $image .'"></a>
                </div>
                <div class="message">
                    <span class="author"><a href="' . $account . '"  target="_blank">' . $name . '</a></span>: ' . 
                    $tweet . 
                    '<span class="time"> - ' . $time . '</span>
                </div>
            </div>';
        }
    }
    else
        $output = '<div class="tweet"><span class="error">' . curl_error($ch) . '</span></div>';

    curl_close($ch);
    return $output;
}

}

?

  • 写回答

1条回答 默认 最新

  • doutao5419 2013-02-11 14:49
    关注

    You need to parse the output from that function, using a loop and a DOM library, more specifically the tweet itself, which is nested inside of several divs/spans.

    You can then do a INSERT INTO statement for each iteration of the loop.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度