We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4711
    • 0 Posts
    Hi there!

    I’m doing something wrong but I don’t know what.. Is there anyone who can help me with this?

    This piece of code only outputs my last tweet. I think there is something wrong with parseChunk in my snippet.

    <?php
    # Snippet to return Twitter feed from a XML Feed
    # USAGE: [[getTweets? &username=`username` &limit=`3`]]
     
    if (!isset($username) || $username == '') return 'Please enter a username';
    if (!isset($limit) || $limit == 0) $limit = 10;
    $json = file_get_contents('http://twitter.com/status/user_timeline/'. $username .'.json?count='. $limit .'', true);
    $decode = json_decode($json, true);
    
    if($decode != '') {
    	for($i=0;$i<$limit;$i++) {
    		$modx->setPlaceholder('twitterText', $decode[$i][text]);
    		$modx->setPlaceholder('twitterDate', $decode[$i][created_at]);
    		return $modx->parseChunk('tTweetsHome');
    	}
    }
    else {
    	$modx->setPlaceholder('twitterText', 'Twitter information unavailable..');
    	return $modx->getChunk('tTweetsHome');
    }
    
      • 3914
      • 12 Posts
      the problem is that you do a return in your for-loop. So at the first execution of the loop, the return exits the loop and you have only one tweet.
      Also, when studying the twitter API I found out the correct url to retrieve te tweets. See https://dev.twitter.com/console for more info about that.

      I used your snippet code and changed it as follows (note: I use a different chunk name):
      <?php
      # Snippet to return Twitter feed from a XML Feed
      # USAGE: [[getTweets? &username=`username` &limit=`3`]]
       
      if (!isset($username) || $username == '') return 'Please enter a username';
      if (!isset($limit) || $limit == 0) $limit = 10;
      
      $json = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username, true);
      $decode = json_decode($json, true);
      
      if($decode != '') {
      	$htmlTweets = '';
      	for($i=0;$i<$limit;$i++) {
      		$modx->setPlaceholder('twitterText', $decode[$i][text]);
      		$modx->setPlaceholder('twitterDate', $decode[$i][created_at]);
      		$htmlTweets .= $modx->parseChunk('singleTweet');
      	}
      	return $htmlTweets;
      }
      else {
      	$modx->setPlaceholder('twitterText', 'Twitter information unavailable..');
      	return $modx->getChunk('singleTweet');
      }
      
        • 4711
        • 0 Posts
        Thanks for the reply!
        I’m glad to hear that you can use the script for your own project!!

        I just changed a few things. (Added a function for the mentions and hashes)
        Feel free to use it..

        <?php
        # Snippet to return Twitter info from a JSON Feed
        # USAGE: [[getTweets? &username=`username` &limit=`3`]]
        
        function twitterlinks($tweet) {
        	$tweet = preg_replace('/(^|\s)@(\w+)/', '\1<a href="http://www.twitter.com/\2" rel="external">@\2</a>', $tweet);
        	return preg_replace('/(^|\s)#(\w+)/', '\1<a href="http://search.twitter.com/search?q=%23\2" rel="external">#\2</a>', $tweet);
        }
         
        if (!isset($username) || $username == '') return 'Please enter a username..';
        if (!isset($limit) || $limit == 0) $limit = 11;
        $json = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name='. $username, true);
        $decode = json_decode($json, true);
        
        if($decode != '') {
        	$count = count($decode);
        	for($i=0;$i<$limit;$i++) {
        		$twitterText = twitterlinks($decode[$i][text]);
        		$modx->setPlaceholder('twitterText', $twitterText);
        		$modx->setPlaceholder('twitterDate', $decode[$i][created_at]);
        		$output .= $modx->parseChunk('tTweetsHome');
        	}
        	return $output;
        }
        else {
        	return 'Twitter information unavailable..';
        }
          • 3914
          • 12 Posts
          Well I am glad we could help each other this way. By the way I am not sure if I will use this method to retrieve a twitterfeed, because twitter is not very fast and I must put the tweets on the frontpage of a website. This way, the page rendering is waiting for the tweets being received. I’ll build a solution with AJAX after page load I think.
            • 32316
            • 387 Posts
            Last fall I used used a jquery based twitter feed - had to fix it once, just needed to upload a new version of the script, I think twitter changed the length of a number and it broke the math (rounding errors)
            Anyway it was easy to incorporate and works http://tweet.seaofclouds.com/

            You can see my implementation here: http://olneyfriends.org/summit/join.html