2013-02-11 21 views
0

我正在使用開放源代碼twitter.class.php在Twitter上查詢/搜索特定術語。保存鳴叫而不是回顯/列出項目

我不想回應一切。我想將輸出保存到數據庫。

如何替換回顯並將每條記錄保存到數據庫?

我不知道如何打破getTweets($ termt,100);

100使它一次得到100個推文。我不知道如何使用一個數組。請幫幫我。

謝謝。

這是我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; 
} 

}

+0

你可以使用對象,做捲曲請求,使用正則表達式,但不知道如何使用數組? http://php.net/array並開始閱讀。 – 2013-02-11 14:56:37

回答

0

您需要使用循環和DOM庫(更具體地說是推文本身)解析該函數​​的輸出,該推文本身嵌套在多個div/spans內。

然後,您可以爲循環的每次迭代執行INSERT INTO語句。

+0

或者如果你覺得特別像黑客一樣,你可以修改或擴展twitter類以具有相同的功能,但是它在'output ='的位置,你會做一個數據庫插入語句。 – Husman 2013-02-11 14:51:19

+0

我使用Twitter關閉了一個解決方法。 $ feed =「http://search.twitter.com/search.atom?q=monday&rpp=95」; $ xml = simplexml_load_file($ feed); ($ i = 0; $ i <94; $ i ++){ \t $ title = $ xml-> entry [$ i] - > content; \t \t // MYSQL保存功能GOES HERE // THIS WAY我得到保存每個RECORD(鳴叫)到數據庫 \t \t} ?> 總之,謝謝你們。 – user1581579 2013-02-11 16:23:11

+0

偉大的工作,這正是你將要與圖書館有關。所以它可以工作。 – Husman 2013-02-11 16:41:53