We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12013
    • 61 Posts
    Just found this as an alternative, seems to be working so far.

    http://modxcms.com/forums/index.php/topic,34977.0.html
      • 18913
      • 654 Posts
      I was having a problem with simplexml_load_file too, on a site that didn’t have much in the way of tweet traffic. It appears that twitters "search" site doesn’t keep all the history, just the most recent. And for a relatively inactive account that could mean them not being found.

      So I ended up adapting my version of fetchTweets (above) to this. Hope it helps ...
      Matt

      Usage :
      [!fetchTweets? &twaccount=`15454940` &twnumber=`3`!]

      <?php
      //fetchTweets
      //Snippet author : Matt Considine 12/3/2010
      //Based on http://scriptplayground.com/tutorials/php/Twitter-Integration-Class-Using-PHP5-and-cURL/
      
      class Twitter
      {
        public $tweets = array();
        public function __construct($user, $limit = 5)
        {
          $user = str_replace(' OR ', '%20OR%20', $user);
          $feed = curl_init('http://twitter.com/statuses/user_timeline/' . $user . '.atom?count=' . $limit);
          curl_setopt($feed, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($feed, CURLOPT_HEADER, 0);
          $xml = curl_exec($feed);
          curl_close($feed);
          $result = new SimpleXMLElement($xml);
          foreach($result->entry as $entry)
          {
              $tweet = new stdClass();
              $tweet->id = (string) $entry->id;
              $user = explode(' ', $entry->author->name);
              $tweet->user = (string) $user[0];
              $tweet->author = (string) substr($entry->author->name, strlen($user[0])+2, -1);
              $tweet->title = (string) $entry->title;
      
                     $twit = (string) $entry->content;
      
                     //Remove the preceding 'username: '
                     $twit = substr(strstr($twit, ': '), 2, strlen($twit));
      
                     // Convert URLs into hyperlinks
                     $twit = preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/",
                            "<a href=\"\\0\" rel=\"nofollow\" target=\"_blank\">\\0</a>", $twit);  //MattC
      
                     // Convert usernames (@) into links
                     $twit = preg_replace("(@([a-zA-Z0-9\_]+))",
                          "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">\\0</a>", $twit);
      
                     // Convert hash tags (#) to links
                     $twit = preg_replace('/(^|\s)#(\w+)/',
                          '\1<a href="http://search.twitter.com/search?q=%23\2"  target=\"_blank\">#\2</a>', $twit);
      
                     //Specifically for non-English tweets, converts UTF-8 into ISO-8859-1
                     $twit = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $twit);
      
      
              $tweet->content = (string) $twit;
              $tweet->updated = (int) strtotime($entry->updated);
              $tweet->permalink = (string) $entry->link[0]->attributes()->href;
              $tweet->avatar = (string) $entry->link[1]->attributes()->href;
              array_push($this->tweets, $tweet);
          }
          unset($feed, $xml, $result, $tweet);
        }
        public function getTweets() { return $this->tweets; }
      }
      
      if (!function_exists('humanTiming')) {
      function humanTiming ($origltime)  //MattC from stackoverflow.com
      {
          $timehurdle = 2*3600; //MattC e.g. anything older than 2 hours will just show the timestamp
      
          $time = time() - $origltime; // to get the time since that moment
      
          $tokens = array (
              31536000 => 'year',
              2592000 => 'month',
              604800 => 'week',
              86400 => 'day',
              3600 => 'hour',
              60 => 'minute',
              1 => 'second'
          );
      
          foreach ($tokens as $unit => $text) {
              if ($time < $unit) continue;
              $numberOfUnits = floor($time / $unit);
              if ($time < $timehurdle)  //MattC
              {
              $timestamp = 'about '.$numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';  //MattC
              }
              else
              {
                //date_default_timezone_set('America/New_York');
                $timestamp = date('M j, Y h:i:s A T',$origltime);  //MattC
              }
              return $timestamp; //MattC
          }
      }
      } //if exists
      
      $feed = new Twitter($twaccount, $twnumber);  //change this for feed and number desired
      $tweets = $feed->getTweets();
      
      $twit = "";
      $twit .= '<ul class="twitter-updates">';
      foreach ($tweets as $tweet)
      {//available fields : id, user, author, title, content, updated, permalink, avatar
        $twit .= '<li>';
        //$twit .= 'Id : '.$tweet->id.' User : '.$tweet->user.' Title : '.$tweet->title;
        //$twit .= '<img src="'.$tweet->avatar.'" />';
        $twit .= '<span class="update">' . $tweet->content . '</span>';
        //$twit .= ' by '.$tweet->author;
        $twit .= ' <span class="date"><a href="'.$tweet->permalink.'"  style="display:block;color:#666666;font-size:11px;"  target="_blank">' .humanTiming($tweet->updated). '</a></span>';  //MattC ala CNN
        $twit .= '</li>';
      }
      $twit .= '</ul>';
      
      return $twit;
      ?>
      
        • 6537
        • 70 Posts
        Thanks for sharing that Matt - it looks like it’s ticking over ok at the moment, although there has just been a couple of blips where only an empty <ul></ul> is output. I guess that might be the feed though. I’ve been testing with the Twitter Twitter feed, which might explain that.

        I have also dumped this into a snippet: http://bavotasan.com/tutorials/how-to-create-a-twitter-feed-on-your-web-site/

        Haven’t bothered making it anymore ’MODX friendly’ yet with snippet parameters etc, but as-is seems to be working.

        Dan
          • 33238
          • 388 Posts
          i had something similar, and the problem has the twitter user, was wrong and does.n exist.

          i check the right name and everything works well undecided

          i know is stupid but just check it. grin
            --
            ysanmiguel.com
            • 36830
            • 140 Posts
            is there any way of preserving the tweets front-end longer/indefinitely? if there's no activity for a while, the output dies out and leaves a blank space...